Node.js · interactive
The event loop, phase by phase
Synchronous code finishes first. Queued callbacks then run in a fixed order. Turn some off and run the loop again.
Call stack
empty
nextTick queue
process.nextTick
Promise queue
Promise.then
Timers
setTimeout(0)
I/O (poll)
fs.readFile callback
Check
setImmediate
Run one turn and watch callbacks leave their queues for the call stack.
Queue callbacks, then run one turn.
1 The call stack runs to the end. Nothing queued can start yet.
console.log("start")
console.log("end")2 3 4 5 6
This is the order when the timer is already due and the file callback is already ready. If the timer is still waiting, poll can run first.
In production
Tools and technologies
One thread interleaves work with I/O callbacks; similar patterns appear outside Node too.
Not tied to one language — use these names when you search docs or design a system.
- libuvSoftware
Cross-platform async I/O library (used by Node and others).
GitHub ↗ - Node.jsPlatform
Reference runtime for the phases shown in this visual.
Official site ↗ - TokioSoftware
Rust async runtime: tasks, timers, and I/O drivers.
GitHub ↗ - asyncioSoftware
Python’s event loop for coroutines and non-blocking I/O.
Official site ↗