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.