Browser Microtasks and Rendering
A microtask is a small piece of JavaScript work the browser runs as soon as the current JavaScript callback finishes, before it starts another task or usually updates the page. A microtask checkpoint is the browser’s process of repeatedly draining that work until the microtask queue is empty.
This is the mechanism behind promise callbacks, queueMicrotask(), and mutation observer callbacks. It gives asynchronous code a predictable place to continue without making the browser switch immediately to unrelated work or paint a half-updated state.
The problem microtasks solve
JavaScript in a browser runs callbacks one at a time. A user click, timer, network event, or script execution is a task: a unit of work selected by the browser’s event loop. While a task is running, no other JavaScript callback runs on that thread.
Suppose promise callbacks were run synchronously. Then this code would execute the callback before the current function returned, making promises behave unpredictably and allowing callbacks to interrupt the code that created them. Suppose instead they were always ordinary tasks. The browser could run unrelated input handlers, timers, or rendering work between a promise settling and its continuation.
Microtasks provide a middle ground: promise continuations are asynchronous, but they run promptly and as a group after the current task finishes.
A task, then a microtask checkpoint
A simplified turn of a browser event loop looks like this:
- The browser chooses a task, such as a click handler or timer callback.
- JavaScript runs that task to completion.
- The browser performs a microtask checkpoint.
- If appropriate, the browser updates styles and layout and paints a new frame.
- The event loop chooses another task.
The browser has discretion about when to render. It does not necessarily paint after every task, and it may perform checkpoints at other points required by the platform. But the important practical rule is that microtasks queued by a task are normally drained before the browser gets an opportunity to render the result of that task or run a later task.
At a checkpoint, the browser takes the oldest microtask from the queue and runs it. If that callback queues another microtask, the new callback is added to the queue. The browser continues until the queue is empty; it does not run only the callbacks that existed when the checkpoint began.
Consider this example:
console.log("start");
setTimeout(() => console.log("timer"), 0);
Promise.resolve().then(() => console.log("promise"));
queueMicrotask(() => console.log("microtask"));
console.log("end");
The output is normally:
start
end
promise
microtask
timer
The timer is a later task. The promise reaction and the explicit microtask run during the checkpoint after the current script task, so both happen before that timer can run. The promise callback was queued first, which is why it precedes the callback passed to queueMicrotask().
This does not mean every timer always runs immediately after the checkpoint. The browser can select another eligible task first. It means the timer cannot run while the current task and its checkpoint are still in progress.
Promise callbacks are microtasks
Calling .then(), .catch(), or .finally() registers a promise reaction, meaning code to run when a promise fulfills or rejects. When the relevant promise settles, the browser queues that reaction as a microtask. Even an already-settled promise queues its callback; it does not invoke it synchronously.
Microtasks generally run in queue order. A callback that queues more work goes behind microtasks already waiting:
queueMicrotask(() => {
console.log("first");
queueMicrotask(() => console.log("nested"));
});
queueMicrotask(() => console.log("second"));
The result is first, second, then nested. The nested callback was added after second was already in the queue. All three still run within the same checkpoint, before the browser proceeds to the next task.
This ordering is why a chain of await operations can appear to run without giving the page a chance to respond. After an awaited promise is already settled, the continuation resumes through a promise reaction microtask. Each continuation can schedule the next one.
Rendering happens after the drain
Rendering is not a callback that runs after every line of JavaScript. The browser waits for a suitable rendering opportunity, then may recalculate styles, perform layout, and paint. Microtask checkpoints come before that opportunity in the normal task-to-render sequence.
For example, changing the DOM in a click handler and then queueing a microtask means the microtask runs before the browser usually paints the DOM change. This lets a library finish related bookkeeping or apply a second change before the user sees an intermediate state. A requestAnimationFrame() callback is associated with the browser’s rendering update and is typically run near the next frame, after preceding task work and microtasks. Exact scheduling remains browser-controlled, so code should not rely on a paint occurring at a particular time.
How a microtask chain starves the page
The queue is drained until empty, not for a fixed number of callbacks. Code that continually adds another microtask can therefore prevent the event loop from reaching the next task or rendering opportunity:
function keepRunning() {
queueMicrotask(keepRunning);
}
keepRunning();
Each callback is short, and the JavaScript stack does not grow as it would with ordinary direct recursion. But the queue is never empty. As a result, timers do not fire, input handlers do not run, and the browser cannot paint the page. The tab may look frozen even though the code appears to be doing asynchronous work.
This is microtask starvation. Promise-heavy retry loops, recursive queueMicrotask() calls, and long chains of immediately resolved promises can produce it accidentally. To yield back to the browser, schedule a later task—for example with setTimeout()—or otherwise arrange work in bounded batches. A task boundary gives the browser a chance to process input and, when it chooses, render a frame.
The useful mental model is simple: tasks provide the larger turns of the event loop; microtasks finish the current turn before the browser moves on. That ordering makes promise code consistent, but it also means an unbounded microtask queue can monopolize the page.