Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Add the Microtask Queue

In this lesson, you added one box to the event loop picture, the microtask queue, and used it to explain why a .then() handler on a promise that’s already fulfilled runs before a timer that was set first.

Key Points

  • In the picture you know, your code runs on the call stack, a timer waits in the waiting area, its callback joins the task queue when it’s due, and the event loop moves one task across when the stack is empty.
  • In museum_order.mjs, the timer is set first with a delay of zero, yet the handler on kioskReady, a promise already fulfilled through Promise.resolve(), logs before it. Node waits at least one millisecond even for a delay of zero, so the run alone doesn’t say whether the handler was ready first or gets priority.
  • Node keeps a second queue, the microtask queue. A promise’s handler joins it once its promise is fulfilled, never the task queue. A handler on a promise that’s already fulfilled has nothing to wait for, so it goes straight into the microtask queue.
  • When the stack is empty, the event loop checks the microtask queue before it moves a task. It runs every microtask, one after another, until that queue is empty. Only then does it move the next task, even if a timer’s callback is already due and waiting in the task queue.
  • At an await, the async function always steps aside, even when the promise it awaits is already fulfilled. When that promise is fulfilled, the rest of the function joins the microtask queue. In greetFamily(), that’s straight away, so it joins behind the handler that was queued first.

The rule is that whenever the call stack is empty, the event loop runs every microtask, then moves the next task. Promise handlers and the code after an await are microtasks. Timer callbacks are tasks. So a timer that’s set first can still run last.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Here’s something odd: a handler on a promise that’s already fulfilled runs before a timer that was set first, even with a delay of zero. In this video, you’ll add one box to the event loop picture you know, and that box explains why.

00:16 Here’s that picture: your code runs on the call stack. A timer waits in the waiting area, outside your code, where Node keeps track of it. When it’s due, its callback joins the task queue.

00:29 And the event loop asks one question: is the stack empty? If it is, the loop moves one task across.

00:39 Here’s museum_order.mjs, a short script for the family’s day. First, it sets a timer with a delay of zero, to remind Grandma Bisi about her tea break. Next, Promise.resolve() makes kioskReady, a promise that’s already fulfilled.

00:57 Then it attaches a handler that logs the kiosk’s message. Last, the script logs that the plan has started.

01:05 Run it, and the script’s own line comes first. Next comes the handler’s line. The timer’s line comes last.

01:15 But the timer was set first, and its delay was zero. Even for a delay of zero, Node waits at least one millisecond. So did the handler go first because it was ready first, or because it gets priority?

01:28 The picture you know can’t say, because it’s missing something.

01:35 Node keeps a second queue, the microtask queue. A promise’s handler joins it once its promise is fulfilled, never the task queue. And the event loop treats it differently.

01:48 Watch the run again with the new box. The script goes on the stack. It sets the timer, which goes to the waiting area. The promise is already fulfilled, so the handler has nothing to wait for.

02:00 It goes straight into the microtask queue. The script logs its line, ends, and the stack is empty.

02:08 Now the event loop asks its question, and the answer is yes. Say the timer is due by now, so its callback joins the task queue. Even so, the handler goes first.

02:20 Before the loop moves a task, it checks the microtask queue. It moves the handler onto the stack, and the handler logs the kiosk’s message. The loop runs every microtask in that queue, one after another, until it’s empty.

02:34 Only then does it move the next task, the timer’s callback, which logs the tea break.

02:41 Now add an async function, greetFamily, to the script. It awaits kioskReady, then logs a welcome for the Adeyemis. The script calls it after attaching the handler, before its own log.

02:56 The timer is still set first. Your turn: pause the video and predict the order the four lines print in.

03:09 In this run, the script’s line comes first. Then the handler’s line, then the welcome. The tea break is still last. Here’s why: the script calls greetFamily, which goes on the stack.

03:23 It reaches its await and steps aside, even though kioskReady is already fulfilled. An await always steps aside, the same way the handler didn’t run straight away.

03:34 So the rest of greetFamily joins the microtask queue, behind the handler. When the stack is empty, the loop runs both microtasks, in the order they joined.

03:45 Only then does the timer’s callback get its turn.

03:49 Here’s the rule. Whenever the call stack is empty, the event loop runs every microtask, until that queue is empty. Only then does it move the next task. Promise handlers and the code after an await are microtasks.

04:04 Timer callbacks are tasks.

04:08 So a timer that’s set first can still run last. Next, in Predict the Order of Timers and Promises, you’ll use this rule on more puzzles, and run each one to check your answer.

Become a Member to join the conversation.