Skip to solution
mediumPhone Screen

What does queueMicrotask() do, and how does its callback ordering compare to a resolved Promise .then() and setTimeout(fn, 0)?

499 views
01

Understand the problem

Question presented to candidate: "Say I call queueMicrotask, then Promise.resolve().then, then setTimeout with a delay of 0, in that exact order. What order do the three callbacks actually run in, and why?"

What a strong answer should cover:

  • queueMicrotask() schedules a callback on the SAME microtask queue that Promise .then callbacks use -- it is not a separate, lower-priority queue.
  • setTimeout(fn, 0) schedules a macrotask, which never runs until the current microtask queue is completely empty, no matter how many more microtasks get added along the way.
  • Ordering among microtasks follows scheduling order (FIFO), not API identity -- whichever of queueMicrotask or .then was scheduled first runs first.
  • A microtask that itself schedules another microtask (including a nested queueMicrotask call) still runs before the next macrotask, because the engine keeps draining the microtask queue until it is genuinely empty.
  • Real motivation for queueMicrotask existing: it lets code schedule microtask-timed work without allocating a throwaway Promise just to get access to .then.

Clarifying questions expected:

  • "Is this the browser event loop or the Node.js event loop specifically -- Node has an extra process.nextTick queue that runs even earlier than both."

Code / implementation expected: Yes -- a short, runnable snippet demonstrating the actual print order.

event-loopmicrotaskqueuemicrotask
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript event loop and async-ordering interview questions. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the p

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSMicrotask vs macrotask ordering, including a nested microtask (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 89 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track