In Add the Microtask Queue, a .then() handler on a promise that was already fulfilled ran before a timer that was set first. The event loop picture you know gained a second queue to explain it, and you can now use that picture to solve more than one puzzle.
In this lesson, you’ll use it to predict the output order of scripts that mix the script’s own logs, promise handlers, and timers, then run each one to check. You’ll also queue work of your own and see what happens when microtasks never stop coming.
Predict a Puzzle With Two Queues
To predict the output order, you need the rule in words you can check a script against. Your code runs on the call stack, where each function call waits for the calls it makes to return. A timer waits in the waiting area until it’s due, and then its callback joins the task queue.
A promise’s fulfillment handler joins the microtask queue when the promise is fulfilled, and its rejection handler when it’s rejected, never the task queue. Attach a handler to a promise that’s already settled, and it joins right away. The code after an await joins the microtask queue too, and an await always steps aside, even on a promise that’s already fulfilled.
The event loop decides what runs next. Whenever the call stack is empty, the event loop runs every microtask until that queue is empty. Only then does it run the next task. That’s the two-queue rule, pictured here with both queues:
Here’s a puzzle for the family’s day that puts the rule to work. It sets the tea break reminder first, as before. This time, the handler on kioskReady sets a second timer for Tolu’s gift shop visit and returns a note, and a second .then() logs that note. Save it as museum_puzzle.mjs:
museum_puzzle.mjs
setTimeout(() => {
console.log("Timer: Tea break for Grandma Bisi");
}, 0);
const kioskReady = Promise.resolve("Kiosk ready");
kioskReady
.then((message) => {
console.log(`Then: ${message}`);
setTimeout(() => {
console.log("Timer: Gift shop for Tolu");
}, 0);
return "Map printed";
})
.then((note) => {
console.log(`Then: ${note}`);
});
console.log("Script: Plan started");
Before you run it, write down the order of the five lines. Two questions decide it. When does the second handler join the microtask queue? And where does the gift shop timer wait while the handlers run?
Both handlers run before either timer:
$ node museum_puzzle.mjs
Script: Plan started
Then: Kiosk ready
Then: Map printed
Timer: Tea break for Grandma Bisi
Timer: Gift shop for Tolu
The second handler isn’t in the microtask queue when the loop starts draining it. Its promise, the one the first .then() returns, is fulfilled with "Map printed" only when the first handler returns, so that’s when the second handler joins. The loop runs every microtask until the queue is empty, including ones that join while it drains, so the second handler still goes before any task.
The gift shop timer is set inside the first handler, so it waits in the waiting area like any timer. Even for a delay of zero, Node waits at least one millisecond. The tea break timer was set earlier, so it’s due first, and both timer callbacks run only after the microtask queue is empty.