Promise.all()は本当に並列処理しているのか
⇒Promise.all()は本当に並列処理している
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 |
main(); function main() { const p = console.log; const getTimeoutPromise = timespan_ms => { return new Promise(resolve => { setTimeout(() => { resolve("timespan_ms : " + timespan_ms); }, timespan_ms); }); }; const sub = () => { const ps = []; ps.push(getTimeoutPromise(50)); ps.push(getTimeoutPromise(40)); ps.push(getTimeoutPromise(100)); ps.push(getTimeoutPromise(30)); ps.push(getTimeoutPromise(20)); ps.push(getTimeoutPromise(10)); const start = new Date(); new Promise(resolve => { Promise.all(ps).then(results => { resolve(results); }); }).then(result => { p(result); const end = new Date(); const timelapse = end.getTime() - start.getTime(); p("timelapse : " + timelapse + "[ms]"); }); }; sub(); } |
1 2 3 4 5 6 7 |
[ 'timespan_ms : 50', 'timespan_ms : 40', 'timespan_ms : 100', 'timespan_ms : 30', 'timespan_ms : 20', 'timespan_ms : 10' ] timelapse : 103[ms] |