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 |
'use strict'; const start = new Date(); const callback = () => { const end = new Date(); const lapseMs = end.getTime() - start.getTime(); return `${lapseMs}ミリ秒後`; }; const createTimeoutPromise = (ms, callback) => { return new Promise(resolve => { setTimeout(() => { resolve(callback()); }, ms); }); }; const asyncAwaitVariables = async (ms, callback) => { const results = []; const promise0 = createTimeoutPromise(ms, callback); const promise1 = createTimeoutPromise(ms, callback); const promise2 = createTimeoutPromise(ms, callback); results.push(await promise0); results.push(await promise1); results.push(await promise2); console.log('asyncAwaitVariables', results); }; const asyncAwaitDirect = async (ms, callback) => { const results = []; results.push(await createTimeoutPromise(ms, callback)); results.push(await createTimeoutPromise(ms, callback)); results.push(await createTimeoutPromise(ms, callback)); console.log('asyncAwaitDirect...', results); }; const main = () => { const ms = 100; asyncAwaitVariables(ms, callback); asyncAwaitDirect(ms, callback); }; main(); |
1 2 |
asyncAwaitVariables [ '101ミリ秒後', '101ミリ秒後', '101ミリ秒後' ] asyncAwaitDirect... [ '104ミリ秒後', '205ミリ秒後', '305ミリ秒後' ] |