A Deeper Event Loop Mental Model

The event loop should be understood as coordination between execution jobs and host scheduling mechanisms rather than as one universal queue. Browsers and Node.js have different host-level details, but the common model is that JavaScript executes jobs, asynchronous operations complete in the host, and continuations become eligible for later execution.

A long-running synchronous task prevents queued work from running. Likewise, repeatedly scheduling microtasks can delay tasks because the runtime must process the microtask queue before moving to another task.

Microtask Starvation
let count = 0;

function loop() {
  count++;
  if (count < 100000) {
    queueMicrotask(loop);
  }
}

setTimeout(() => console.log('timer'), 0);
loop();
💡 The timer cannot execute until the currently scheduled microtasks have been processed. Excessive microtask scheduling can therefore delay ordinary tasks.
Key Takeaways
  • The event loop coordinates JavaScript jobs with host scheduling.
  • Long synchronous work blocks subsequent JavaScript execution.
  • Excessive microtasks can delay task execution.
Knowledge Check

Why can a long chain of microtasks delay a timer?