AsyncGeneratorFunction() constructor
The AsyncGeneratorFunction() constructor creates AsyncGeneratorFunction objects.
> [!WARNING]
> The arguments passed to this constructor are dynamically parsed and executed as JavaScript.
> APIs like this are known as [injection sinks](/docs/Web/API/Trusted_Types_API#concepts_and_usage), and are potentially a vector for [cross-site-scripting (XSS)](/docs/Web/Security/Attacks/XSS) attacks.
>
> You can mitigate this risk by always passing TrustedScript objects instead of strings and [enforcing trusted types](/docs/Web/API/Trusted_Types_API#using_a_csp_to_enforce_trusted_types).
>
> See [Security considerations](/docs/Web/JavaScript/Reference/Global_Objects/Function/Function#security_considerations) in the `Function()` constructor reference for more information.
The **`AsyncGeneratorFunction()`** constructor creates AsyncGeneratorFunction objects.
Note that `AsyncGeneratorFunction` is not a global object. It could be obtained by evaluating the following code.
```js
const AsyncGeneratorFunction = async function* () {}.constructor;
```
The `AsyncGeneratorFunction()` constructor is not intended to be used directly, and all caveats mentioned in the Function() description apply to `AsyncGeneratorFunction()`.
## Syntax
```js-nolint
new AsyncGeneratorFunction(functionBody)
new AsyncGeneratorFunction(arg1, functionBody)
new AsyncGeneratorFunction(arg1, arg2, functionBody)
new AsyncGeneratorFunction(arg1, arg2, /* …, */ argN, functionBody)
AsyncGeneratorFunction(functionBody)
AsyncGeneratorFunction(arg1, functionBody)
AsyncGeneratorFunction(arg1, arg2, functionBody)
AsyncGeneratorFunction(arg1, arg2, /* …, */ argN, functionBody)
```
> [!NOTE]
> `AsyncGeneratorFunction()` can be called with or without [`new`](/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncGeneratorFunction` instance.
### Parameters
See Function().
## Examples
Note that these examples omit the use of trusted types for brevity. For code showing the recommended approach, see [Using `TrustedScript`](/docs/Web/JavaScript/Reference/Global_Objects/eval#using_trustedscript) in `eval()`.
### Using the constructor
The following example uses the `AsyncGeneratorFunction` constructor to create an async generator function.
```js
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
## See also
- [`async function*`](/docs/Web/JavaScript/Reference/Statements/async_function*)
- [`async function*` expression](/docs/Web/JavaScript/Reference/Operators/async_function*)
- [`Function()` constructor](/docs/Web/JavaScript/Reference/Global_Objects/Function/Function)
- [Iterators and generators](/docs/Web/JavaScript/Guide/Iterators_and_generators) guide
- Functions
Cite this
"AsyncGeneratorFunction() constructor". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGeneratorFunction/AsyncGeneratorFunction JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy