The Node.js Runtime ExplainedLesson 1.2
How the Node.js event loop works step by step
event loop phases, call stack, callback queue, microtask queue, setTimeout vs setImmediate, process.nextTick
The Event Loop Phases
The event loop is a continuous cycle that processes pending work in six phases. Most application code runs in poll (I/O callbacks) and timers (setTimeout, setInterval).
Execution Order
console.log('1 - synchronous');
setTimeout(() => console.log('4 - timer'), 0);
Promise.resolve().then(() => console.log('3 - microtask'));
process.nextTick(() => console.log('2 - nextTick'));
console.log('1b - still synchronous');
// Output order: 1, 1b, 2, 3, 4Priority order: the current call stack runs to completion first. Then process.nextTick callbacks drain before Promise microtasks. Only then does the event loop advance to the next phase (timers, I/O).
Why This Matters
If you flood process.nextTick recursively, you starve the event loop — I/O callbacks never run. Use setImmediate when you want to yield to I/O between chunks of work. setImmediate fires in the check phase, after the poll phase, giving pending I/O a chance to execute first.
