Hard

Node.js Event Loop Phases

Deep dive into libuv, the interaction between microtasks and macrotasks, and `setImmediate` vs `process.nextTick`.
30 min read14 Jan 2026

Solution

Order of Execution

  1. Call Stack: Sync code runs first.
  2. process.nextTick: Runs immediately after the current operation completes, before the event loop continues. It can starve the I/O loop if abused.
  3. Microtasks (Promises): Run after nextTick but before rendering/macrotasks.
  4. Macrotasks (Timers/IO):
    • Timers Phase: setTimeout
    • Poll Phase: I/O callbacks (fs, http)
    • Check Phase: setImmediate

Summary

process.nextTick has the highest priority of async operations. setImmediate runs after I/O callbacks in the "Check" phase, making it distinct from setTimeout(0).