The Async
Function
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 could be obtained by evaluating the following code.
Object.getPrototypeOf(async function(){}).constructor
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
arg1, arg2, ... argN
x
", "theValue
", or "a,b
".functionBody
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 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 a 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.
AsyncFunction.length
AsyncFunction
constructor's length property whose value is 1.AsyncFunction.prototype
AsyncFunction
prototype objectAsyncFunction.constructor
AsyncFunction
.AsyncFunction.prototype[@@toStringTag]
AsyncFunction
instancesAsyncFunction
instances inherit methods and properties from AsyncFunction.prototype
. As with all constructors, you can change the constructor's prototype object to make changes to all AsyncFunction
instances.
AsyncFunction
constructorfunction resolveAfter2Seconds(x) { return new Promise(resolve => { setTimeout(() => { resolve(x); }, 2000); }); } var AsyncFunction = Object.getPrototypeOf(async function(){}).constructor var a = new AsyncFunction('a', 'b', 'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);'); a(10, 20).then(v => { console.log(v); // prints 30 after 4 seconds });
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (ECMA-262) The definition of 'AsyncFunction object' in that specification. | Living Standard | Initial definition in ES2017. |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 55 | ? | 52 | No | 42 | ? |
prototype |
55 | ? | 52 | No | 42 | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 55 | 55 | ? | 52 | No | 42 | ? |
prototype |
? | 55 | ? | 52 | No | 42 | ? |
async function function
async function expression
Function
function statement
function expression
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction