AsyncFunction() Konstruktor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2017.
Der AsyncFunction()
Konstruktor erstellt AsyncFunction
Objekte.
Beachten Sie, dass AsyncFunction
kein globales Objekt ist. Es kann mit dem folgenden Code erhalten werden:
const AsyncFunction = async function () {}.constructor;
Der AsyncFunction()
Konstruktor ist nicht dafür vorgesehen, direkt genutzt zu werden, und alle in der Beschreibung von Function()
erwähnten Vorbehalte gelten auch für AsyncFunction()
.
Syntax
new AsyncFunction(functionBody)
new AsyncFunction(arg1, functionBody)
new AsyncFunction(arg1, arg2, functionBody)
new AsyncFunction(arg1, arg2, /* …, */ argN, functionBody)
AsyncFunction(functionBody)
AsyncFunction(arg1, functionBody)
AsyncFunction(arg1, arg2, functionBody)
AsyncFunction(arg1, arg2, /* …, */ argN, functionBody)
Note:
AsyncFunction()
kann mit oder ohnenew
aufgerufen werden. Beide erstellen eine neueAsyncFunction
Instanz.
Parameter
Siehe Function()
.
Beispiele
Erstellen einer asynchronen Funktion mit einem AsyncFunction() Konstruktor
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
const AsyncFunction = async function () {}.constructor;
const fn = new AsyncFunction(
"a",
"b",
"return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);",
);
fn(10, 20).then((v) => {
console.log(v); // prints 30 after 4 seconds
});
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-async-function-constructor |
Browser-Kompatibilität
BCD tables only load in the browser