The AsyncFunction constructor creates a new async function object. In JavaScript, every asynchronous function is actually an AsyncFunction object.

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

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


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.


argN Optional

Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript parameter (any of plain identifier, rest parameter, or destructured parameter, optionally with a default), or a list of such strings separated with commas.

As the parameters are parsed in the same way as function declarations, whitespace and comments are accepted. For example: "x", "theValue = 42", "[a, b] /* numbers */" — or "x, theValue = 42, [a, b] /* numbers */". ("x, theValue = 42", "[a, b]" is also correct, though very confusing to read.)


A string containing the JavaScript statements comprising the function definition.


Async function objects created with the AsyncFunction constructor are parsed when the function is created. This is less efficient than declaring an async function with an async function expression and calling it within your code, because such functions are parsed with the rest of the code.

All arguments passed to the function, except the last, are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Note: async functions created with the AsyncFunction constructor do not create closures to their creation contexts; they are always created in the global scope.

When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the AsyncFunction constructor was called.

This is different from using eval with code for an async function expression.

Invoking the AsyncFunction constructor as a function (without using the new operator) has the same effect as invoking it as a constructor.


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-objects

Browser compatibility

BCD tables only load in the browser

See also