-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-for.js
49 lines (38 loc) · 876 Bytes
/
async-for.js
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
function sleep (time) {
return new Promise ((resolve, reject) => {
setTimeout(() => {
return resolve();
}, time);
});
}
async function main () {
let p = [
1000,
1000,
1000
];
console.time('for-each');
p.forEach(async (time) => {
await sleep(time);
});
console.timeEnd('for-each');
console.time('for-of');
for (let time of p) {
await sleep(time);
}
console.timeEnd('for-of');
console.time('for-map');
let ps = p.map((time) => {
return sleep(time);
});
await Promise.all(ps);
console.timeEnd('for-map');
console.time('for-await');
let now = Date.now();
let pss = p.map(sleep);
for await (let s of pss) {
console.log('paso', Date.now() - now);
}
console.timeEnd('for-await');
}
main();