Advertisement
Advanced JavaScript, Event Loop & Concurrency

What is 'starvation' of the macrotask queue, and how can recursive microtasks cause it?

Short answer

Because ALL pending microtasks are drained before the next macrotask runs, code that keeps scheduling new microtasks from within a microtask (e.g.

Save JavaScript mock interview
Advertisement
AModel answer
Because ALL pending microtasks are drained before the next macrotask runs, code that keeps scheduling new microtasks from within a microtask (e.g. a Promise chain that queues another .then() indefinitely) can starve macrotasks — including rendering and setTimeout callbacks — from ever running.
function foreverMicrotask() {
  Promise.resolve().then(foreverMicrotask); // never lets a macrotask run
}
foreverMicrotask(); // freezes rendering indefinitely
Real-world example
Debugging a page that becomes completely unresponsive due to a recursive .then() chain with no exit condition.
Likely follow-ups
  • How would you rewrite this to periodically yield to macrotasks?
Related topics