Microtasks
Microtasks are jobs that run after the current call stack empties and before the next macrotask (or paint). Promise reactions, queueMicrotask, and await continuations are microtasks.
Detailed Explanation
The event loop's important sentence: when a task finishes, drain the entire microtask queue, including microtasks that those microtasks enqueue, before taking another task. That is why Promise.resolve().then(fn) runs before setTimeout(fn, 0), and why an unbounded chain of .then can freeze a page without ever throwing.
Sources of microtasks:
promise.then/catch/finallyreactionsawaitafter the awaited value settles (the rest of the async function)queueMicrotask(fn)MutationObservercallbacks in browsers- In Node,
process.nextTickis a separate queue that drains before the promise microtask queue, still before the next macrotask
queueMicrotask is the explicit API. Use it when you want "after this stack, before timers and I/O", without allocating a promise. Promise.resolve().then(fn) is the older spelling and creates a promise.
Because the queue drains fully, a microtask that always schedules another microtask never yields to rendering or to setTimeout. That is a livelock at the loop level. Break the chain with setTimeout(0), setImmediate (Node), or MessageChannel to hop to a task.
await is not "go to the event loop's next task". await on an already-fulfilled promise still schedules the continuation as a microtask. await 1 yields to other microtasks, not to timers.
What it costs you: surprising order in logs, and UI that never paints because you did all your work in promise chains. Batching libraries (Vue, React) often flush updates in a microtask; a setTimeout test can see a different state than a queueMicrotask test.
Reach for a microtask to keep follow-up work in the same turn (normalise state, notify observers) without waiting for a timer. Reach for a task when you must let the browser render or process input. Never spin microtasks in a tight recursive .then.
Code Examples
console.log("1 sync");
queueMicrotask(() => console.log("3 microtask"));
Promise.resolve().then(() => console.log("4 then"));
setTimeout(() => console.log("5 timeout"), 0);
console.log("2 sync");
// => 1 sync 2 sync 3 microtask 4 then 5 timeoutDraining: each then enqueues another microtask before any timeout.
// Draining: each then enqueues another microtask before any timeout.
Promise.resolve()
.then(() => console.log("a"))
.then(() => console.log("b"));
setTimeout(() => console.log("c"), 0);
// => a b casync function alreadyResolved() {
console.log("before");
await 1;
console.log("after await");
}
alreadyResolved();
queueMicrotask(() => console.log("sibling microtask"));
// => before sibling microtask after awaitCommon Gotchas
An unbounded microtask chain starves timers and paint
the tab locks with no `RangeError`. Yield with a task.
`process.nextTick` is not a promise microtask
it runs earlier, and a `nextTick` loop starves promises too. Prefer `queueMicrotask` or `setImmediate` in Node.
`await` on a fulfilled value still hops a microtask
it is not synchronous after the `await` line.