W3cubDocs

/JavaScript

AsyncGeneratorFunction

The AsyncGeneratorFunction creates a new async generator function object. In JavaScript, every async generator function is actually an AsyncGeneratorFunction object.

Note that AsyncGeneratorFunction is not a global object. It could be obtained by evaluating the following code.

const AsyncGeneratorFunction = (async function* () {}).constructor;

Syntax

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

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

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

Parameters

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.)

functionBody

A String containing the JavaScript statements comprising the function definition.

Description

Async generator function objects created with the AsyncGeneratorFunction constructor are parsed when the function is created. This is less efficient than declaring a generator 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 generator functions created with the AsyncGeneratorFunction 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 AsyncGeneratorFunction constructor was called.

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

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

Examples

Using the constructor

The following example uses the AsyncGeneratorFunction constructor to create an async generator function.

const AsyncGeneratorFunction = (async function* () {}).constructor;
const createAsyncGenerator = new AsyncGeneratorFunction('a', 'yield a * 2');
const asyncGen = createAsyncGenerator(10);
asyncGen.next().then((res) => console.log(res.value)); // 20

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
AsyncGeneratorFunction
63
79
55
No
50
12
63
63
55
46
12
8.0
1.0
10.0.0
8.10.0

See also

© 2005–2022 MDN 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/AsyncGeneratorFunction