Microtask checkpoints and ordering
How it works
queueMicrotask(callback) requests a host microtask. In browsers, checkpoints drain the microtask queue, including newly queued microtasks, subject to the checkpoint algorithm. Promise reactions also use this scheduling mechanism. An endlessly replenished microtask queue can starve later tasks; use bounded work and yield to task scheduling when responsiveness matters. A thrown queueMicrotask callback is reported differently from a thrown promise handler.
Example 1
queueMicrotask(() => { console.log("A"); queueMicrotask(() => console.log("C")); });Promise.resolve().then(() => console.log("B"));console.log("S");Expected output in the tested environment:
SABCC is appended behind the already queued B reaction.
Example 2
Promise.resolve().then(() => { throw new Error("bad"); }).catch(e => console.log(e.message));queueMicrotask(() => console.log("queued"));Expected output in the tested environment:
queuedbadThe catch reaction is queued after the fulfillment handler throws.
Common mistake
queueMicrotask does not yield to browser painting by itself. Recursively queueing microtasks can delay rendering.
Practice
Add two nested microtasks, writing the expected queue contents after each callback. Keep the chain finite.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Microtask checkpoints and ordering — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
Where is a microtask scheduled from another microtask normally placed?
Why can an unbounded microtask chain hurt responsiveness?
What happens when a then fulfillment handler throws?
For Microtask checkpoints and ordering, what is the console output of example 1 in Node.js 22.16.0?
queueMicrotask(() => { console.log("A"); queueMicrotask(() => console.log("C")); });Promise.resolve().then(() => console.log("B"));console.log("S");For Microtask checkpoints and ordering, what is the console output of example 2 in Node.js 22.16.0?
Promise.resolve().then(() => { throw new Error("bad"); }).catch(e => console.log(e.message));queueMicrotask(() => console.log("queued"));