# forWithWait
const iterator = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i < 5) return { value: this.i++, done: false };
// return Promise.resolve({ value: this.i++, done: false });
// return Promise.resolve({ donw: true });
return { done: true };
},
};
},
};
// console.log(Array.from([...iterator]));
// console.log(new Set(iterator));
// for (let value of iterator) {
// console.log(value);
// }
const asyncIterator = {
[Symbol.asyncIterator]() {
return {
i: 0,
next() {
if (this.i < 5)
return Promise.resolve({ value: this.i++, done: false });
return Promise.resolve({ done: true });
},
};
},
};
(async function() {
for await (let value of asyncIterator) {
console.log(value);
}
})();
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
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