-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathasynciterator.jsdoc
More file actions
89 lines (67 loc) · 1.83 KB
/
Copy pathasynciterator.jsdoc
File metadata and controls
89 lines (67 loc) · 1.83 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
AsyncIterator : AsyncIterable
<p>
An AsyncIterator is an Object that returns a sequence of Promises.
</p>
<p>
Since AsyncIterators are AsyncIterables, you can use **for await (const value of iterable) {}**
to easily loop over the values in an AsyncIterator.
</p>
<p>
Calling an async generator function (**async function* () {}**) returns
an AsyncIterator.
</p>
<p>
See %%/AsyncIterable|AsyncIterable%% for more details.
</p>
<example>
const sleep = t => new Promise(r => setTimeout(r, t));
const stream = async function*() {
for (let i = 0; i < 3; i++) {
await sleep(300 * i);
yield i;
}
};
const iterator = stream();
console.dir(await iterator.next());
console.dir(await iterator.next());
console.dir(await iterator.next());
console.dir(await iterator.next());
// for-await-of loops make it easy to loop over async iterables
for await (const i of stream()) {
console.log(i);
}
</example>
Spec:
http://www.ecma-international.org/ecma-262/10.0/#sec-asynciterator-interface
----
instance.next([yieldValue : Object]) : Promise
Promise return type:
{ value : Object, done : Boolean }
----
instance.return([yieldValue : Object]) : Promise
----
instance.throw([rejectReason : Object]) : Promise
----
instance[Symbol.asyncIterator] : Function<AsyncIterator>
Returns a method that returns **this**.
<example>
const sleep = t => new Promise(r => setTimeout(r, t));
const stream = async function*() {
for (let i = 0; i < 3; i++) {
await sleep(300 * i);
yield i;
}
};
const iterator = stream();
var iteratorIterator = iterator[Symbol.asyncIterator]();
console.log(iterator === iteratorIterator);
console.log();
for await (const x of stream()) {
console.log(x);
}
console.log();
// Since AsyncIterators are also AsyncIterables, you can use for-await-of loops with them.
for await (const x of iterator) {
console.log(x);
}
</example>