-
Notifications
You must be signed in to change notification settings - Fork 22.5k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.