JS AsyncFunction
JS AsyncFunction
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
Syntax
new AsyncFunction([arg1[, arg2[, ...argN]],] functionBody)
Parameters
arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each
must be a string that corresponds to a valid JavaScript identifier or a
list of such strings separated with a comma; for example "x",
"theValue", or "a,b".
functionBody
A string containing the JavaScript statements comprising the function
definition.
Description
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.
AsyncFunction instances
AsyncFunction 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.
Examples
Creating an async function from an AsyncFunction constructor
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}