js async iterator

Solutions on MaxInterview for js async iterator by the best coders in the world

showing results for - "js async iterator"
María Camila
22 Feb 2017
1let range = {
2  from: 1,
3  to: 5,
4
5  [Symbol.asyncIterator]() { // (1)
6    return {
7      current: this.from,
8      last: this.to,
9
10      async next() { // (2)
11
12        // note: we can use "await" inside the async next:
13        await new Promise(resolve => setTimeout(resolve, 1000)); // (3)
14
15        if (this.current <= this.last) {
16          return { done: false, value: this.current++ };
17        } else {
18          return { done: true };
19        }
20      }
21    };
22  }
23};
24
25(async () => {
26
27  for await (let value of range) { // (4)
28    alert(value); // 1,2,3,4,5
29  }
30
31})()
32