AsyncFunction() constructor

The AsyncFunction() constructor creates AsyncFunction objects.

Note that AsyncFunction is not a global object. It can be obtained with the following code:

const AsyncFunction = async function () {}.constructor;

The AsyncFunction() constructor is not intended to be used directly, and all caveats mentioned in the Function() description apply to AsyncFunction().


new AsyncFunction(functionBody)
new AsyncFunction(arg0, functionBody)
new AsyncFunction(arg0, arg1, functionBody)
new AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody)

AsyncFunction(arg0, functionBody)
AsyncFunction(arg0, arg1, functionBody)
AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody)

Note: AsyncFunction() can be called with or without new. Both create a new AsyncFunction instance.



Creating an async function from an AsyncFunction() constructor

function resolveAfter2Seconds(x) {
  return new Promise((resolve) => {
    setTimeout(() => {
    }, 2000);

const AsyncFunction = async function () {}.constructor;

const fn = new AsyncFunction(
  "return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);",

fn(10, 20).then((v) => {
  console.log(v); // prints 30 after 4 seconds


ECMAScript Language Specification
# sec-async-function-constructor

Browser compatibility

BCD tables only load in the browser

See also