-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
63 lines (43 loc) · 1.93 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
title: Symbol.asyncIterator
slug: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator
page-type: javascript-static-data-property
browser-compat: javascript.builtins.Symbol.asyncIterator
---
{{JSRef}}
The **`Symbol.asyncIterator`** static data property represents the [well-known symbol](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#well-known_symbols) `@@asyncIterator`. The [async iterable protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) looks up this symbol for the method that returns the async iterator for an object. In order for an object to be async iterable, it must have an `@@asyncIterator` key.
{{EmbedInteractiveExample("pages/js/symbol-asynciterator.html", "taller")}}
## Value
The well-known symbol `@@asyncIterator`.
{{js_property_attributes(0, 0, 0)}}
## Examples
### User-defined async iterables
You can define your own async iterable by setting the `[Symbol.asyncIterator]` property on an object.
```js
const myAsyncIterable = {
async *[Symbol.asyncIterator]() {
yield "hello";
yield "async";
yield "iteration!";
},
};
(async () => {
for await (const x of myAsyncIterable) {
console.log(x);
}
})();
// Logs:
// "hello"
// "async"
// "iteration!"
```
When creating an API, remember that async iterables are designed to represent something _iterable_ — like a stream of data or a list —, not to completely replace callbacks and events in most situations.
### Built-in async iterables
There is no object in the core JavaScript language that is async iterable. Some web APIs, such as {{domxref("ReadableStream")}}, have the `Symbol.asyncIterator` method set by default.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Iteration protocols](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols)
- [for await...of](/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of)