How does the event loop order tasks, microtasks, and rendering?
Reason about observable ordering without relying on slogans such as “promises always run first.”
Useful prerequisites
JavaScript usually runs your code on one main thread in the browser. So how can a page respond to clicks while waiting for a network request? The event loop coordinates when queued callbacks run. Tasks and microtasks are two different queues in that scheduling process, and their order affects what happens before the browser gets a chance to render.
JavaScript usually runs your code on one main thread in the browser. So how can a page respond to clicks while waiting for a network request? The event loop coordinates when queued callbacks run. Tasks and microtasks are two different queues in that scheduling process, and their order affects what happens before the browser gets a chance to render.
The quick version
A task runs to completion. After it finishes, the browser drains the microtask queue completely (including any microtasks that were queued while draining). Only then can it consider a rendering opportunity or pick up the next task. Rendering is not guaranteed after every task — a long task or a microtask queue that never empties can delay paint indefinitely.
Build the mental model
The event loop is the browser’s way of interleaving different kinds of work: script tasks, timer callbacks, network responses, user input, and the chance to paint. It does not run everything in a single FIFO queue.
There are multiple task sources (timers, networking, user interaction, etc.). The browser decides which task to run next according to HTML’s event-loop rules, not a simple global order. What is reliable is the relationship between a task and its microtasks:
- The current task runs until it returns.
- A microtask checkpoint runs. The queue is drained fully. Any microtask that schedules another microtask is also run before the checkpoint ends.
- Only after the microtask queue is empty may the browser update the rendering or move to another task.
This is why a tight loop of queueMicrotask or recursive Promise.then can freeze the page — the checkpoint never finishes, so the browser never gets a chance to paint or handle input.
Rendering opportunities sit between tasks. They are not a special kind of microtask. The browser may skip them under load, or it may paint after a task that did not schedule any microtasks. You cannot treat “after the promise” as “after paint.”
See it in code
console.log('sync');
setTimeout(() => console.log('task'), 0);
Promise.resolve().then(() => {
console.log('microtask 1');
queueMicrotask(() => console.log('microtask 2'));
});
queueMicrotask(() => console.log('microtask 3'));
// Observed order:
// sync
// microtask 1
// microtask 3
// microtask 2
// task
Everything synchronous runs first. Then the microtask checkpoint drains the queue, including the newly queued microtask 2. Only after that does the timer task run.
A more production-relevant version of the same idea:
async function processBatch(items) {
for (const item of items) {
await processItem(item); // each await is a microtask boundary
}
// paint can still be delayed if processItem keeps the microtask queue busy
}
If processItem itself schedules more microtasks or the loop is large, the browser may not paint until the whole batch is finished.
Where it gets tricky
Different task sources are not strictly ordered relative to each other.
A timer scheduled with setTimeout(fn, 0) is not guaranteed to run before or after a network callback that became ready at roughly the same time. The only hard guarantee is the task → microtask checkpoint relationship.
Recursive microtasks starve rendering.
function spin() {
queueMicrotask(spin);
}
spin();
// the page will not paint and will stop responding to input
This is not theoretical — it is a real failure mode when people try to “yield” using microtasks instead of tasks.
MutationObserver callbacks run as microtasks.
They are part of the same checkpoint. A long observer chain can delay the next paint the same way recursive promises can.
requestAnimationFrame is not a microtask.
It is scheduled against the rendering steps. Code that runs inside rAF still participates in the normal task/microtask rules for anything it schedules itself.
Rendering is opportunistic.
The browser may choose to paint after a task even if microtasks ran, or it may delay paint if it is under time pressure. You cannot write correct UI logic that assumes “paint happened right after this promise.”
A common misunderstandings
-
“Promises always run before
setTimeout.”
True for a microtask scheduled in the same task, but not a universal law across different task sources or after a long task. -
“
setTimeout(fn, 0)means run immediately.”
It means “queue a task.” The task only runs after the current task and its microtasks finish. -
“Using promises automatically keeps the UI responsive.”
A microtask queue that never empties is just as blocking as a long synchronous loop.
What this changes in an application
Most “the UI froze for a moment” bugs that involve promises are microtask starvation or long tasks that never yield. If you process a large list with await inside a tight loop, you are still holding the main thread across many microtask checkpoints. The browser never gets a clean chance to paint or handle input.
Practical responses:
- Break long work into tasks (
scheduler.postTask,setTimeout, or the olderMessageChanneltrick) rather than pure microtasks when you need the UI to stay responsive. - Measure with the Performance panel. Look for long tasks and for periods where the main thread is busy with microtasks while frames are dropped.
- Treat “I used promises so it should be fine” as a smell, not a guarantee.
Questions worth exploring next
- Where do MutationObserver callbacks run, and why does that matter?
- Why can a recursive
queueMicrotaskfreeze a page while a recursivesetTimeoutusually does not? - What is the difference between a task and a microtask from the browser’s point of view?
- How does
requestAnimationFramefit into this model?
References and next steps
- JavaScript execution model — the authoritative description of tasks, microtasks, and the event loop.
- HTML Living Standard — event loops — the precise rules the browser is supposed to follow.
- In the Weeds: Microtasks — still one of the clearest explanations of the practical consequences.