AsyncGeneratorFunction() Konstruktor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Der AsyncGeneratorFunction() Konstruktor erstellt AsyncGeneratorFunction Objekte.

Beachten Sie, dass AsyncGeneratorFunction kein globales Objekt ist. Es kann erhalten werden, indem der folgende Code ausgewertet wird.

js
const AsyncGeneratorFunction = async function* () {}.constructor;

Der AsyncGeneratorFunction() Konstruktor ist nicht zur direkten Verwendung gedacht, und alle in der Beschreibung von Function() erwähnten Vorbehalte gelten auch für AsyncGeneratorFunction().

Syntax

js
new AsyncGeneratorFunction(functionBody)
new AsyncGeneratorFunction(arg1, functionBody)
new AsyncGeneratorFunction(arg1, arg2, functionBody)
new AsyncGeneratorFunction(arg1, arg2, /* …, */ argN, functionBody)

AsyncGeneratorFunction(functionBody)
AsyncGeneratorFunction(arg1, functionBody)
AsyncGeneratorFunction(arg1, arg2, functionBody)
AsyncGeneratorFunction(arg1, arg2, /* …, */ argN, functionBody)

Hinweis: AsyncGeneratorFunction() kann mit oder ohne new aufgerufen werden. Beide erzeugen eine neue Instanz von AsyncGeneratorFunction.

Parameter

Siehe Function().

Beispiele

Verwendung des Konstruktors

Das folgende Beispiel verwendet den AsyncGeneratorFunction Konstruktor, um eine asynchrone Generatorfunktion zu erstellen.

js
const AsyncGeneratorFunction = async function* () {}.constructor;
const createAsyncGenerator = new AsyncGeneratorFunction("a", "yield a * 2");
const asyncGen = createAsyncGenerator(10);
asyncGen.next().then((res) => console.log(res.value)); // 20

Spezifikationen

Specification
ECMAScript Language Specification
# sec-asyncgeneratorfunction-constructor

Browser-Kompatibilität

BCD tables only load in the browser

Siehe auch