microsoft / microsoft/TypeScript
Allow augmentation of the async function prototype
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
L’issue non indica alcun file o test del repository; inizia esaminando le interfacce proposte AsyncFunction e AsyncFunctionConstructor insieme alla specifica collegata di ECMAScript async-function-constructor. Risolvi il confine di compatibilità tra async functions e le funzioni che restituiscono Promises; il lavoro è completato quando esiste un design concordato per le implicazioni del breaking-change e il conseguente comportamento di TypeScript.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100