CodingNeed.
Mid · Concurrency

Explain the Event Loop

Predict the order of synchronous code, promise microtasks, and timers. Explain why a long-running callback can delay every timer.

Examples

Compare approaches

Baseline approach

Begin by explaining the core mechanism, stating assumptions, and walking through one concrete example.

Time: Depends on design · Space: Depends on design

console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Strong answer

A, D, C, B. Current synchronous work finishes before microtasks drain; the timer callback is eligible afterward.

Time: Discuss operation costs · Space: Discuss retained state

console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");

Common traps

  • A zero-delay timer is not immediate.
  • State assumptions and justify trade-offs rather than memorizing a single answer.
Practise in the workspace →