What happens when microtasks keep scheduling more microtasks?
AdvancedJavaScript interview questionBy UIRound, updated 21 November 2026
Short answer
The page freezes. After each task, the browser runs microtasks until the queue is empty, and that includes microtasks added while it is draining. A microtask that always queues another one keeps the queue from ever emptying, so the event loop never reaches rendering or the next task: timers never fire, clicks are never handled and nothing repaints. A setTimeout loop does not have this problem, because each callback is a separate task.
Why the loop cannot move on
One turn of the event loop runs one task, then performs a microtask checkpoint, then may render. The checkpoint is a loop of its own: take the oldest microtask, run it, repeat while the queue is not empty. There is no limit on how many microtasks one checkpoint runs, and no point inside it where the browser stops to paint or read input.
Promise reactions (then, catch, finally), the code after an await, queueMicrotask callbacks and MutationObserver callbacks all go into this queue.
A small example
const status = document.querySelector("#status");
let count = 0;
setTimeout(() => console.log("timer fired"), 0);
document.querySelector("button").addEventListener("click", () => console.log("clicked"));
function spin() {
count++;
status.textContent = String(count);
queueMicrotask(spin); // queues the next run before this one ends
}
spin();What you observe:
timer firednever logs, even with a delay of 0.- Clicking the button does nothing. The click is queued as a task that never gets a turn.
#statuskeeps its old text on screen. The DOM value changes on every run, but the browser never reaches a rendering step to paint it.- After a few seconds, the browser offers to stop the unresponsive page.
There is no stack overflow. Each microtask runs on an empty call stack after the previous one returns, so the recursion is not nested. Promise.resolve().then(spin) and an async function with while (true) await null; behave the same way.
Compare it with a setTimeout loop
function tick() {
count++;
status.textContent = String(count);
setTimeout(tick, 0); // queues a task, not a microtask
}This loop also runs forever, but the page stays usable. Each tick is one task, and between tasks the browser handles the click, fires other timers and paints the new number. After a few nested levels, browsers clamp the delay to at least 4 ms, so it also runs far fewer times per second.
The same problem with a finite chain
The loop does not have to be infinite to hurt. Any chain of microtasks runs as one block before the next paint.
async function processAll(items) {
for (const item of items) {
await handle(item); // handle is fast and returns a resolved promise
progress.value++;
}
}The await looks like a pause, but it only queues a microtask. If handle never waits on real I/O, every item runs before the browser paints, and the progress bar jumps from 0 to full. Adding async or await to CPU-bound code does not make it yield.
How to yield properly
To let the browser render and handle input, you must end the current task and continue in a new one.
setTimeout(resolve, 0): works everywhere. The continuation goes to the back of the task queue, and nested timers are clamped to about 4 ms.MessageChannel: posting a message to a port queues a task without the timer clamp. Schedulers such as React's use this. Practise it in Schedule callbacks with MessageChannel.scheduler.yield(): where supported, it yields and resumes your work ahead of other queued tasks of the same priority. Feature-detect it and fall back tosetTimeout.requestAnimationFrame: for visual work, such as an animation step or a layout read before a paint. It runs once per frame and not at all in hidden tabs, so it is not a general yield for background work. See what requestAnimationFrame is used for.
const yieldToMain = () =>
globalThis.scheduler?.yield
? scheduler.yield()
: new Promise((resolve) => setTimeout(resolve, 0));
async function processAll(items) {
let last = performance.now();
for (const item of items) {
handle(item);
progress.value++;
if (performance.now() - last > 40) {
await yieldToMain(); // a real task boundary: paint and input can run here
last = performance.now();
}
}
}Yielding every 40 to 50 ms keeps each task short without paying the cost of a task per item. What is a long task covers how to measure the result, and for work that never needs the DOM, a Web Worker removes it from the main thread entirely.
Practise it
Predict the output of setTimeout, promises, queueMicrotask and await, then the nested event loop order question. Then fix a frozen page in Split a long task into chunks that yield to the browser.
Common follow-up questions
Why are microtasks designed to drain completely?
So that promise code sees a consistent state. When one promise callback resolves another, the next step runs before any event handler or timer can change the DOM or shared data in between. The cost is that microtasks cannot be interrupted.
Can a MutationObserver cause the same freeze?
Yes. Its callback is a microtask. If the callback changes the nodes it observes, it queues another callback, and the loop never ends. Disconnect the observer while you make the change, or check whether the change is still needed.
Does this happen in Node.js?
Yes. A recursive promise chain blocks timers and I/O in Node.js too. Recursive process.nextTick is worse, because the nextTick queue drains before promise microtasks. Use setImmediate to yield to I/O. See setTimeout vs setImmediate vs process.nextTick.
Is await in a loop always a problem?
await in a loop always a problem?No. If each await waits for real I/O, such as a fetch, the function is suspended until a task delivers the result, and the browser renders in between. The problem is only await on values that are already resolved.
Mistakes to avoid
Saying microtasks run "one per turn"
They run until the queue is empty, including new ones.
Expecting
awaitorPromise.resolve()to give the browser a chance to paintUsing
queueMicrotaskto break up heavy workIt makes the work run sooner, not in smaller tasks.
Calling an infinite microtask loop a stack overflow
The stack is empty between microtasks.
Using
requestAnimationFrameto yield in background work, which stops running in hidden tabs
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Predict the event-loop order of
setTimeout, promises,queueMicrotaskandawaitEasy15 minFree - Predict nested event loop order: promises that schedule timers, timers that schedule promises, and
awaitin loopsMedium25 min - Split a long task into chunks that yield to the browser to keep INP lowMedium30 min
- Schedule callbacks with MessageChannel so they run before timersMedium35 min
- Build a Zone.js-style task tracker: pending macrotasks and microtasks,
isStableandwhenStable()Hard55 min