Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 2 OF 6 · 2 MIN READ

The Node.js Event Loop Phases

The Node.js event loop runs in phases: timers, pending callbacks, poll, check and close. Between callbacks it drains process.nextTick and the promise microtask queue. Output-order questions about setTimeout, setImmediate, nextTick and promises are very common.

What you will learn

  • List the event loop phases in order
  • Explain where setTimeout, setImmediate, nextTick and promises run
  • Predict output order of mixed async code
  • Explain starvation from recursive nextTick

Key concepts

Timers phase

Runs setTimeout and setInterval callbacks whose time has passed. The delay is a minimum, not a guarantee.

Poll phase

Waits for and runs I/O callbacks (sockets, file reads). If nothing is pending it may block here until a timer is due.

Check phase

Runs setImmediate callbacks, right after poll.

Microtasks

process.nextTick callbacks run first, then promise callbacks (.then, await continuations). Both queues are drained after each callback, before the loop moves on.

Step by step

The order of one loop iteration

   ┌───────────────────────────┐
┌─>│ timers (setTimeout)       │
│  ├───────────────────────────┤
│  │ pending callbacks         │
│  ├───────────────────────────┤
│  │ poll (I/O)                │
│  ├───────────────────────────┤
│  │ check (setImmediate)      │
│  ├───────────────────────────┤
└──┤ close callbacks           │
   └───────────────────────────┘
 after every callback: nextTick queue, then promise queue

setTimeout(0) vs setImmediate

From the main module the order is not guaranteed: it depends on whether 1 ms has passed when the loop starts. Inside an I/O callback, setImmediate always runs first, because check comes right after poll.

Examples

Predict the output

console.log('1 sync')
setTimeout(() => console.log('5 timeout'), 0)
setImmediate(() => console.log('6 immediate'))
Promise.resolve().then(() => console.log('4 promise'))
process.nextTick(() => console.log('3 nextTick'))
console.log('2 sync')

// 1 sync, 2 sync, 3 nextTick, 4 promise, then 5/6 (order of 5 and 6 may swap here)

import { readFile } from 'node:fs'
readFile(import.meta.filename, () => {
  setTimeout(() => console.log('timeout'), 0)
  setImmediate(() => console.log('immediate')) // always first inside I/O
})

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Predict the output of nested promises inside a setTimeout callback.
  • Show how a recursive process.nextTick starves I/O, then fix it with setImmediate.
  • Explain why await on a resolved promise still yields to other microtasks.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

What is the difference between process.nextTick and setImmediate?

nextTick runs right after the current operation, before promises and before the loop continues. setImmediate runs in the check phase of the next loop iteration. The names are confusing: nextTick is the more immediate one.

How is the Node.js event loop different from the browser one?

Both run a task then drain microtasks. Node.js splits tasks into libuv phases (timers, poll, check, close) and adds nextTick and setImmediate. The browser has a rendering step and requestAnimationFrame, which Node.js does not have.

Summary

Timers → pending → poll → check → close, and after every callback the nextTick queue then the promise queue are drained. Inside I/O callbacks setImmediate beats setTimeout(0), and recursive microtasks can starve the loop.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus