MediumEvent Loop & Promises🐛 Debug Challenge

Sequential awaits for independent operations

Buggy Code — Can you spot the issue?

function delay(ms, val) {
  return new Promise(r => setTimeout(() => r(val), ms));
}

async function run() {
  const a = await delay(50, 'a');
  const b = await delay(50, 'b');
  console.log(a);
  console.log(b);
}

run();

Fixed Code

function delay(ms, val) {
  return new Promise(r => setTimeout(() => r(val), ms));
}

async function run() {
  const [a, b] = await Promise.all([
    delay(50, 'a'),
    delay(50, 'b'),
  ]);
  console.log(a);
  console.log(b);
}

run();

Bug Explained

Bug: Sequential awaits mean b only starts after a resolves. Total time is ~100ms. These are independent and can run in parallel.

Explanation: Promise.all fires both at the same time. Total time is ~50ms. Output order follows the array order, not resolution order.

Key Insight: Sequential awaits for independent operations is a performance bug. Use Promise.all to run independent async operations in parallel.

More Event Loop & Promises Debug Challenges

EasyMissing return in Promise chain drops valueMediumAsync function result is a Promise — not awaited

Practice spotting bugs live →

38 debug challenges with AI hints

🐛 Try Debug Lab