-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-iterator.js
More file actions
68 lines (32 loc) · 939 Bytes
/
async-iterator.js
File metadata and controls
68 lines (32 loc) · 939 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(async()=>{
console.clear();
//data source
var i=0;
var arr=[1,2,3];
setTimeout(fn,1000);
function fn(){
if(i==3){
end();
}else{
read(arr[i++]);
setTimeout(fn,1000);
}
}//fn
function read(value){
iterator.resolve({value});
}//read
function end(){
iterator.resolve({done:true});
}//end
var iterator = asyncIterator();
function asyncIterator(){
var iterator = ()=>{return {next:()=>new Promise(res=>iterator.resolve=res)}};
iterator.resolve = null;
return iterator;
}//asynciterator
var a = {};
a[Symbol.asyncIterator] = iterator;
for await(let x of a){
console.log(x)
}//for
})();