-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path30-example.js
More file actions
52 lines (47 loc) · 992 Bytes
/
30-example.js
File metadata and controls
52 lines (47 loc) · 992 Bytes
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
function wait(period) {
return new Promise(resolve => {
setTimeout(resolve, period);
});
}
function range(from, to) {
return {
[Symbol.iterator]: function() {
return {
current: from,
last: to,
next() {
if (this.current <= this.last) {
return wait(500).then(() => ({
done: false,
value: this.current++
}));
} else {
return wait(500).then(() => ({ done: true }));
}
}
};
}
};
}
const iterable = range(40, 49);
const iterator = iterable[Symbol.iterator]();
function logAndNext(res) {
if (res && !res.done) {
console.log(res.value);
return iterator.next();
}
}
iterator
.next()
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext)
.then(logAndNext);