Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split (Async)?(Generator)?Function class and ctor pages #22778

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: AsyncFunction() constructor
slug: Web/JavaScript/Reference/Global_Objects/AsyncFunction/AsyncFunction
tags:
- Constructor
- JavaScript
- Reference
browser-compat: javascript.builtins.AsyncFunction.AsyncFunction
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we will need BCD for these constructors.

---

{{JSRef}}

The **`AsyncFunction()`** constructor creates a new {{jsxref("AsyncFunction")}} object. In JavaScript, every [async function](/en-US/docs/Web/JavaScript/Reference/Statements/async_function) is actually an `AsyncFunction` object.

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

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

The `AsyncFunction()` constructor is not intended to be used directly, and all caveats mentioned in the {{jsxref("Function/Function", "Function()")}} description apply to `AsyncFunction()`.

## Syntax

```js-nolint
new AsyncFunction(functionBody)
new AsyncFunction(arg0, functionBody)
new AsyncFunction(arg0, arg1, functionBody)
new AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody)
AsyncFunction(functionBody)
AsyncFunction(arg0, functionBody)
AsyncFunction(arg0, arg1, functionBody)
AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody)
```

> **Note:** `AsyncFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncFunction` instance.
### Parameters

See {{jsxref("Function/Function", "Function()")}}.

## Examples

### Creating an async function from an AsyncFunction() constructor

```js
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}

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

const fn = new AsyncFunction(
"a",
"b",
"return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);",
);

fn(10, 20).then((v) => {
console.log(v); // prints 30 after 4 seconds
});
```

## Specifications

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- [`async function` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
- [`async function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function)
- [`Function()` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function)
Original file line number Diff line number Diff line change
Expand Up @@ -10,93 +10,29 @@ browser-compat: javascript.builtins.AsyncFunction

{{JSRef}}

The **`AsyncFunction` constructor** creates a new
{{jsxref("Statements/async_function", "async function", "", 1)}} object. In JavaScript,
every asynchronous function is actually an `AsyncFunction` object.
The **`AsyncFunction`** object provides methods for [async functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function). In JavaScript, every async function is actually an `AsyncFunction` object.

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

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

## Syntax
## Constructor

```js-nolint
new AsyncFunction(functionBody)
new AsyncFunction(arg0, functionBody)
new AsyncFunction(arg0, arg1, functionBody)
new AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody)
- {{jsxref("AsyncFunction/AsyncFunction", "AsyncFunction()")}}
- : Creates a new `AsyncFunction` object.

AsyncFunction(functionBody)
AsyncFunction(arg0, functionBody)
AsyncFunction(arg0, arg1, functionBody)
AsyncFunction(arg0, arg1, /* … ,*/ argN, functionBody)
```

> **Note:** `AsyncFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncFunction` instance.

### Parameters

- `argN` {{optional_inline}}

- : 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](/en-US/docs/Glossary/Identifier), [rest parameter](/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), or [destructured](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) 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.
## Instance properties

## Description
_Also inherits instance properties from its parent {{jsxref("Function")}}_.

[Async function](/en-US/docs/Web/JavaScript/Reference/Statements/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](/en-US/docs/Web/JavaScript/Reference/Operators/async_function) and calling it
within your code, because such functions are parsed with the rest of the code.
- `AsyncFunction.prototype[@@toStringTag]`
- : The initial value of the [`@@toStringTag`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"AsyncFunction"`. This property is used in {{jsxref("Object.prototype.toString()")}}.

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.
## Instance methods

> **Note:** {{jsxref("Statements/async_function", "async functions", "", 1)}} 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 {{jsxref("Global_Objects/eval", "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.

## Examples

### Creating an async function from an AsyncFunction() constructor

```js
function resolveAfter2Seconds(x) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}

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

const fn = new AsyncFunction(
'a',
'b',
'return await resolveAfter2Seconds(a) + await resolveAfter2Seconds(b);',
);

fn(10, 20).then((v) => {
console.log(v); // prints 30 after 4 seconds
});
```
_Inherits instance methods from its parent {{jsxref("Function")}}_.

## Specifications

Expand All @@ -108,9 +44,9 @@ fn(10, 20).then((v) => {

## See also

- {{jsxref("Statements/async_function", "async function", "", 1)}}
- {{jsxref("Operators/async_function", "async function expression", "", 1)}}
- {{jsxref("Global_Objects/Function", "Function")}}
- {{jsxref("Statements/function", "function statement", "", 1)}}
- {{jsxref("Operators/function", "function expression", "", 1)}}
- {{jsxref("Functions", "Functions and function scope", "", 1)}}
- [`async function` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
- [`async function` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function)
- {{jsxref("Function")}}
- {{jsxref("AsyncGeneratorFunction")}}
- {{jsxref("GeneratorFunction")}}
- {{jsxref("Functions", "Functions", "", 1)}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
title: AsyncGeneratorFunction() constructor
slug: Web/JavaScript/Reference/Global_Objects/AsyncGeneratorFunction/AsyncGeneratorFunction
tags:
- Constructor
- ECMAScript 2018
- AsyncGeneratorFunction
- Iterator
- JavaScript
- Reference
browser-compat: javascript.builtins.AsyncGeneratorFunction.AsyncGeneratorFunction
---

{{JSRef}}

The **`AsyncGeneratorFunction()`** constructor creates a new {{jsxref("AsyncGeneratorFunction")}} object. In JavaScript, every [async generator function](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) is actually an `AsyncGeneratorFunction` object.

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 {{jsxref("Function/Function", "Function()")}} description apply to `AsyncGeneratorFunction()`.

## Syntax

```js-nolint
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`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncGeneratorFunction` instance.
### Parameters

See {{jsxref("Function/Function", "Function()")}}.

## Examples

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

{{Specifications}}

## Browser compatibility

{{Compat}}

## See also

- [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*)
- [`async function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function*)
- [`Function()` constructor](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function)
- [Iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
- {{jsxref("Functions", "Functions", "", 1)}}
Original file line number Diff line number Diff line change
@@ -1,79 +1,36 @@
---
title: AsyncGeneratorFunction
slug: Web/JavaScript/Reference/Global_Objects/AsyncGeneratorFunction
tags:
- Constructor
- ECMAScript 2018
- AsyncGeneratorFunction
- Iterator
- JavaScript
- Reference
browser-compat: javascript.builtins.AsyncGeneratorFunction
---

{{JSRef}}

The **`AsyncGeneratorFunction` constructor** creates a new {{jsxref("Statements/async_function*", "async generator function", "", 1)}} object. In JavaScript, every async generator function is actually an `AsyncGeneratorFunction` object.
The **`AsyncGeneratorFunction`** object provides methods for [async generator functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). 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.
Note that `AsyncGeneratorFunction` is _not_ a global object. It can be obtained with the following code:

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

## Syntax
## Constructor

```js-nolint
new AsyncGeneratorFunction(functionBody)
new AsyncGeneratorFunction(arg0, functionBody)
new AsyncGeneratorFunction(arg0, arg1, functionBody)
new AsyncGeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody)
- {{jsxref("AsyncGeneratorFunction/AsyncGeneratorFunction", "AsyncGeneratorFunction()")}}
- : Creates a new `AsyncGeneratorFunction` object.

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

> **Note:** `AsyncGeneratorFunction()` can be called with or without [`new`](/en-US/docs/Web/JavaScript/Reference/Operators/new). Both create a new `AsyncGeneratorFunction` instance.

### Parameters

- `argN` {{optional_inline}}

- : 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](/en-US/docs/Glossary/Identifier), [rest parameter](/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters), or [destructured](/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) 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 {{jsxref("String")}} containing the JavaScript statements comprising the function definition.
## Instance properties

## Description
_Also inherits instance properties from its parent {{jsxref("Function")}}_.

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](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) and calling it within your code, because such functions are parsed with the rest of the code.
- `AsyncGeneratorFunction.prototype[@@toStringTag]`
- : The initial value of the [`@@toStringTag`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toStringTag) property is the string `"AsyncGeneratorFunction"`. This property is used in {{jsxref("Object.prototype.toString()")}}.
- `AsyncGeneratorFunction.prototype.prototype`
- : All async generator functions share the same [`prototype`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/prototype) property, which is [`AsyncGenerator.prototype`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator). When the function is called, this object becomes the prototype of the returned async generator object. An async generator function instance can also create its own `prototype` property, which will be used instead of `AsyncGeneratorFunction.prototype.prototype`.

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.
## Instance methods

> **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 {{jsxref("Global_Objects/eval", "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.

```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
```
_Inherits instance methods from its parent {{jsxref("Function")}}_.

## Specifications

Expand All @@ -85,12 +42,9 @@ asyncGen.next().then((res) => console.log(res.value)); // 20

## See also

- {{jsxref("Statements/function*", "function*")}}
- {{jsxref("Statements/async_function*", "async function*")}}
- [`function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/function*)
- {{jsxref("Global_Objects/AsyncGenerator", "AsyncGenerator")}}
- {{jsxref("Global_Objects/Generator", "Generator")}}
- {{jsxref("Global_Objects/GeneratorFunction", "GeneratorFunction")}}
- {{jsxref("Global_Objects/AsyncFunction", "AsyncFunction")}}
- [Iterators and generators](/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
- [`async function*` declaration](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*)
- [`async function*` expression](/en-US/docs/Web/JavaScript/Reference/Operators/async_function*)
- {{jsxref("Function")}}
- {{jsxref("AsyncFunction")}}
- {{jsxref("GeneratorFunction")}}
- {{jsxref("Functions", "Functions", "", 1)}}
Loading