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
| function asyncFunction(delay, memo) { console.log(`in`, memo); const thisIsPromise = new Promise((resolve) => { setTimeout(() => { console.log(`done`, memo); resolve(delay); }, delay); });
return thisIsPromise; }
const fetchData = asyncFunction.bind(null, 1000, 'fetchData'); const fetchHeavyData = asyncFunction.bind(null, 3000, 'fetchHeavyData');
function add(a, b) { return a + b; }
async function main() { console.log(`start main`); console.time(`main`);
const a = await fetchHeavyData(); const b = await fetchData();
const result = add(a, b); console.log(`main result`, result); console.timeEnd(`main`); }
async function main2() { console.log(`start main2`); console.time(`main2`);
const a = fetchHeavyData(); const b = fetchData();
const promiseResultArr = await Promise.all([a, b]);
const result = add(...promiseResultArr); console.log(`main2 result`, result); console.timeEnd(`main2`); }
main();
|