πŸ”΄ HardEvent Loop & PromisesπŸ’» Output Question

Promise.all vs sequential await performance

πŸ’‘

Hint

Sequential awaits for independent operations multiply latency. Promise.all is the correct tool when operations are independent β€” latency = max of all, not sum.

What does this output?

async function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function sequential() {
  const start = Date.now();
  await delay(100);
  await delay(100);
  await delay(100);
  return Date.now() - start;
}

async function parallel() {
  const start = Date.now();
  await Promise.all([delay(100), delay(100), delay(100)]);
  return Date.now() - start;
}

sequential().then(ms => console.log('sequential:', ms > 250));
parallel().then(ms => console.log('parallel:', ms < 150));

Correct Output

sequential: true
parallel: true

Why this output?

Explanation: Sequential awaits run one after another: ~300ms total. Promise.all fires all three simultaneously β€” they overlap β€” ~100ms total. sequential > 250ms = true, parallel < 150ms = true.

Key Insight: Sequential awaits for independent operations multiply latency. Promise.all is the correct tool when operations are independent β€” latency = max of all, not sum.

More Event Loop & Promises Output Questions

🟑 MediumClassic setTimeout vs Promiseβ†’πŸ”΄ HardNested setTimeout and Promiseβ†’πŸŸ‘ Mediumasync/await execution orderβ†’πŸ”΄ HardPromise chaining orderβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz