Node.js Event Loop
What is the Event Loop?
The event loop is what makes Node.js non-blocking and efficient.
It handles asynchronous operations by delegating tasks to the system and processing their results through callbacks, allowing Node.js to manage thousands of concurrent connections with a single thread.
How the Event Loop Works
Node.js follows these steps to handle operations:
- Execute the main script (synchronous code)
- Process any microtasks (Promises, process.nextTick)
- Execute timers (setTimeout, setInterval)
- Run I/O callbacks (file system, network operations)
- Process setImmediate callbacks
- Handle close events (like socket.on('close'))
Example: Event Loop Order
console.log('First');
setTimeout(() => console.log('Third'), 0);
Promise.resolve().then(() => console.log('Second'));
console.log('Fourth');
Try it Yourself »
This demonstrates the execution order:
- Sync code runs first ('First', 'Fourth')
- Microtasks (Promises) run before the next phase ('Second')
- Timers execute last ('Third')
Event Loop Phases
The event loop processes different types of callbacks in this order:
- Timers:
setTimeout
,setInterval
- I/O Callbacks: Completed I/O operations
- Poll: Retrieve new I/O events
- Check:
setImmediate
callbacks - Close: Cleanup callbacks (like
socket.on('close')
)
Note: Between each phase, Node.js runs microtasks (Promises) and process.nextTick
callbacks.
Example: Event Loop Phases
console.log('1. Start');
// Next tick queue
process.nextTick(() => console.log('2. Next tick'));
// Microtask queue (Promise)
Promise.resolve().then(() => console.log('3. Promise'));
// Timer phase
setTimeout(() => console.log('4. Timeout'), 0);
// Check phase
setImmediate(() => console.log('5. Immediate'));
console.log('6. End');
Try it Yourself »
The output will be:
1. Start 6. End 2. Next tick 3. Promise 4. Timeout 5. Immediate
This shows the priority order: sync code > nextTick > Promises > Timers > Check phase.
Why is the Event Loop Important?
The event loop enables Node.js to handle thousands of concurrent connections with a single thread, making it perfect for:
- Real-time applications
- APIs and microservices
- Data streaming
- Chat applications
Summary
- Node.js uses an event loop to handle async operations
- Different types of callbacks have different priorities
- Microtasks (Promises) run before the next event loop phase
- This non-blocking model enables high concurrency