microsoft / microsoft/TypeScript

Allow augmentation of the async function prototype

Offen
#36,177 2 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue nennt keine Repository-Dateien oder Tests; beginne damit, die vorgeschlagenen AsyncFunction- und AsyncFunctionConstructor-Schnittstellen zusammen mit der verlinkten ECMAScript async-function-constructor-Spezifikation zu prüfen. Kläre die Kompatibilitätsgrenze zwischen async functions und Funktionen, die Promises zurückgeben; die Aufgabe ist abgeschlossen, wenn ein abgestimmtes Design für die Auswirkungen der breaking-change und das daraus resultierende TypeScript-Verhalten vorliegt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.