microsoft / microsoft/TypeScript
Allow augmentation of the async function prototype
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
AsyncFunction
Suggestion
In JavaScript, We can get constructor of async function via
const AsyncFunction= (async()=>{}).constructor;
And we can do something interesting with it's prototype:
AsyncFunction.prototype.callConcurrently = async function (concurrency,...args){
return Promise.all(Array(concurrency).fill(this).map(f=>f(...args)));
}
Then we can do
const af = async ()=>{};
af.callConcurrently(6, 'foo', 'bar');
But in TypeScript, we have no AsyncFunction interface to do the same thing.
We can add an AsyncFunction interface extends Function and let all functions start with async keyword implement it. And add an AsyncFunctionConstructor interface let (async ()=>{}).constructor implements it. The mode is now new but just like GeneratorFunction and AsyncGeneratorFunction.
Use Cases and Example
interface AsyncFunction
{
callConcurrently(concurrency:Number, ...args:any[]):Promise<any[]>;
}
(async()=>{}).constructor.prototype.callConcurrently = async function (concurrency, ...args){
return Promise.all(Array(concurrency).fill(this).map(f=>f(...args)));
}
const af = async ()=>{};
af.callConcurrently(6, 'foo', 'bar');
Maybe Breaking Change
Currently async()=>3 and ()=>Promise.resolve(3) has the same type in TypeScript.
This proposal will make them different.
They are different in ECMAScript actually. See https://tc39.es/ecmascript-asyncawait/#async-function-constructor . So this breaking change actually fill the gap between TypeScript and ECMAScript.
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals 6
- This feature would agree with the rest of TypeScript's Design Goals 11.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no repository files or tests; start by reviewing the proposed AsyncFunction and AsyncFunctionConstructor interfaces alongside the linked ECMAScript async-function-constructor specification. Resolve the compatibility boundary between async functions and functions returning Promises; done requires an agreed design for the breaking-change implications and resulting TypeScript behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100