Back to results for “Event Loop & Concurrency”
JavaScript › Event Loop & Concurrency

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

Advanced

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.

Common follow-ups: How would you rewrite this to periodically yield to macrotasks?