-
Notifications
You must be signed in to change notification settings - Fork 22.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document hidden Iterator/AsyncIterator object (#25892)
- Loading branch information
Showing
30 changed files
with
288 additions
and
70 deletions.
There are no files selected for viewing
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
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
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
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
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
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
54 changes: 54 additions & 0 deletions
54
.../web/javascript/reference/global_objects/asynciterator/@@asynciterator/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,54 @@ | ||
--- | ||
title: AsyncIterator.prototype[@@iterator]() | ||
slug: Web/JavaScript/Reference/Global_Objects/AsyncIterator/@@asyncIterator | ||
page-type: javascript-instance-method | ||
browser-compat: javascript.builtins.AsyncIterator.@@asyncIterator | ||
--- | ||
|
||
{{JSRef}} | ||
|
||
The **`[@@asyncIterator]()`** method of an `AsyncIterator` object implements the [async iterable protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) and allows built-in async iterators to be consumed by most syntaxes expecting async iterables, such as [`for await...of`](/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) loops. It returns the value of [`this`](/en-US/docs/Web/JavaScript/Reference/Operators/this), which is the async iterator object itself. | ||
|
||
{{EmbedInteractiveExample("pages/js/map-prototype-@@iterator.html")}} | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
asyncIterator[Symbol.asyncIterator]() | ||
``` | ||
|
||
### Return value | ||
|
||
The value of [`this`](/en-US/docs/Web/JavaScript/Reference/Operators/this), which is the async iterator object itself. | ||
|
||
## Examples | ||
|
||
### Iteration using for await...of loop | ||
|
||
Note that you seldom need to call this method directly. The existence of the `@@asyncIterator` method makes all built-in async iterators [async iterable](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols), and iterating syntaxes like the `for await...of` loop automatically calls this method to obtain the async iterator to loop over. | ||
|
||
```js | ||
const asyncIterator = (async function* () { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
})(); | ||
(async () => { | ||
for await (const value of asyncIterator) { | ||
console.log(value); | ||
} | ||
})(); | ||
// Logs: 1, 2, 3 | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- [`for await...of`](/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of) |
64 changes: 64 additions & 0 deletions
64
files/en-us/web/javascript/reference/global_objects/asynciterator/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,64 @@ | ||
--- | ||
title: AsyncIterator | ||
slug: Web/JavaScript/Reference/Global_Objects/AsyncIterator | ||
page-type: javascript-class | ||
browser-compat: javascript.builtins.AsyncIterator | ||
--- | ||
|
||
{{JSRef}} | ||
|
||
An **`AsyncIterator`** object is an object that conforms to the [async iterator protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) by providing a `next()` method that returns a promise fulfilling to an iterator result object. The `AsyncIterator.prototype` object is a hidden global object that all built-in async iterators inherit from. It provides an [`@@asyncIterator`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator/@@asyncIterator) method that returns the async iterator object itself, making the async iterator also [async iterable](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). | ||
|
||
Note that `AsyncIterator` is _not_ a global object, although it will be in the future with the [async iterator helpers proposal](https://github.com/tc39/proposal-async-iterator-helpers). The `AsyncIterator.prototype` object shared by all built-in async iterators can be obtained with the following code: | ||
|
||
```js | ||
const AsyncIteratorPrototype = Object.getPrototypeOf( | ||
Object.getPrototypeOf(Object.getPrototypeOf((async function* () {})())), | ||
); | ||
``` | ||
|
||
## Description | ||
|
||
Currently, the only built-in JavaScript async iterator is the {{jsxref("AsyncGenerator")}} object returned by [async generator functions](/en-US/docs/Web/JavaScript/Reference/Statements/async_function*). There are some other built-in async iterators in web API, such as the one of a {{domxref("ReadableStream")}}. | ||
|
||
Each of these async iterators have a distinct prototype object, which defines the `next()` method used by the particular async iterator. All of these prototype objects inherit from `AsyncIterator.prototype`, which provides am [`@@asyncIterator`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator) method that returns the async iterator object itself, making the async iterator also [async iterable](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). | ||
|
||
> **Note:** `AsyncIterator.prototype` does not implement [`@@iterator`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator), so async iterators are not [sync iterable](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) by default. | ||
## Instance methods | ||
|
||
- [`AsyncIterator.prototype[@@asyncIterator]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator/@@asyncIterator) | ||
- : Returns the async iterator object itself. This allows async iterator objects to also be async iterable. | ||
|
||
## Examples | ||
|
||
### Using an async iterator as an async iterable | ||
|
||
All built-in async iterators are also async iterable, so you can use them in a `for await...of` loop: | ||
|
||
```js | ||
const asyncIterator = (async function* () { | ||
yield 1; | ||
yield 2; | ||
yield 3; | ||
})(); | ||
(async () => { | ||
for await (const value of asyncIterator) { | ||
console.log(value); | ||
} | ||
})(); | ||
// Logs: 1, 2, 3 | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{JSxRef("Statements/async_function*", "async function*")}} | ||
- [The Iterator protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) |
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
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
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
48 changes: 48 additions & 0 deletions
48
files/en-us/web/javascript/reference/global_objects/iterator/@@iterator/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,48 @@ | ||
--- | ||
title: Iterator.prototype[@@iterator]() | ||
slug: Web/JavaScript/Reference/Global_Objects/Iterator/@@iterator | ||
page-type: javascript-instance-method | ||
browser-compat: javascript.builtins.Iterator.@@iterator | ||
--- | ||
|
||
{{JSRef}} | ||
|
||
The **`[@@iterator]()`** method of a `Iterator` object implements the [iterable protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) and allows built-in iterators to be consumed by most syntaxes expecting iterables, such as the [spread syntax](/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) and [`for...of`](/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loops. It returns the value of [`this`](/en-US/docs/Web/JavaScript/Reference/Operators/this), which is the iterator object itself. | ||
|
||
{{EmbedInteractiveExample("pages/js/map-prototype-@@iterator.html")}} | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
iterator[Symbol.iterator]() | ||
``` | ||
|
||
### Return value | ||
|
||
The value of [`this`](/en-US/docs/Web/JavaScript/Reference/Operators/this), which is the iterator object itself. | ||
|
||
## Examples | ||
|
||
### Iteration using for...of loop | ||
|
||
Note that you seldom need to call this method directly. The existence of the `@@iterator` method makes all built-in iterators [iterable](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol), and iterating syntaxes like the `for...of` loop automatically calls this method to obtain the iterator to loop over. | ||
|
||
```js | ||
const arrIterator = [1, 2, 3].values(); | ||
for (const value of arrIterator) { | ||
console.log(value); | ||
} | ||
// Logs: 1, 2, 3 | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- [`for...of`](/en-US/docs/Web/JavaScript/Reference/Statements/for...of) |
Oops, something went wrong.