How Node runs your code · mid
process.nextTick, promises and queueMicrotask
In CommonJS, Node empties the process.nextTick queue before the promise microtask queue after each operation, but in an ES module the top-level code itself runs as a promise job, so promise callbacks queued there run before nextTick callbacks.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Two queues sit outside the libuv phases: the nextTick queue (Node's own) and the microtask queue (V8's: promise reactions, queueMicrotask, the code after an await).
- CommonJS: after the current operation, Node runs every nextTick callback, then every microtask, and repeats until both are empty. Only then does the loop continue.
- ES modules (.mjs or "type": "module"): the module body is evaluated inside a promise job, so V8 keeps draining promise callbacks before Node gets to the nextTick queue. The same snippet prints a different order. Say this when an interviewer asks 'what prints?'.
- await x pauses the async function; the rest runs later as a microtask, even if x is not a promise. Code before the first await runs synchronously.
- Recursive process.nextTick (or an endless promise chain) starves I/O: the loop never reaches the poll phase. The Node docs recommend setImmediate for most deferring.
- Real uses of nextTick: emit an event after a constructor returns so the caller can attach listeners; make a callback API always async instead of 'sometimes sync'.
- queueMicrotask(fn) is the standard, portable way to queue a microtask (it works in browsers too).
The red pen
Common traps: mistake → fix
Mistake: Promises run before nextTick because they have higher 'priority'
Fix: in CommonJS the nextTick queue is drained first; in ES modules top-level promise jobs run first. Explain the queues, not 'priority'.
Mistake: await only pauses for real promises
Fix: await on any value still yields once; the code after it runs as a microtask.
Mistake: nextTick runs on the next loop iteration
Fix: it runs right after the current operation, before the loop moves on.
Easy to hard
Questions interviewers ask, easy to hard
- What is the difference between process.nextTick, queueMicrotask and setImmediate?
- Predict the output of a snippet with nextTick, Promise.then and setTimeout.
- Does the answer change if the file is an ES module? Why?
- How can process.nextTick starve I/O? How would you notice it in production?
- When would you use nextTick in library code?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.