-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split (Async)?(Generator)?Function class and ctor pages
- Loading branch information
Showing
9 changed files
with
285 additions
and
221 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...us/web/javascript/reference/global_objects/asyncfunction/asyncfunction/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
--- | ||
|
||
{{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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...reference/global_objects/asyncgeneratorfunction/asyncgeneratorfunction/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.