The Node.js Event Loop Explained

Iβm Arjun Saxena, a passionate software developer specializing in web engineering. I believe in writing code that creates real solutions to real problems. I love building efficient, user-friendly applications and constantly push myself to learn new technologies. Beyond coding, I enjoy sharing knowledge and growing together with others in the tech community.
βNode.js is single-threadedβ¦ then how does it handle so many users?β
The short answer is π The Event Loop
In this article, weβll understand:
What the Node.js Event Loop is
Blocking vs Non-blocking code
How Node.js handles multiple requests with a single thread
Async code using callbacks and promises
All explained in a very simple & beginner-friendly way.
A Simple Analogy: A Chef Handling Multiple Orders π³
Imagine a restaurant with one chef.
Customers place orders
Some food takes time to cook
The chef does not wait doing nothing
While food is cooking, the chef takes other orders
π This is exactly how Node.js works
| Real Life | Node.js |
| Chef | Single thread |
| Orders | User requests |
| Cooking | Async tasks |
| Order system | Event Loop |
What Is the Node.js Event Loop?
The Event Loop is a system inside Node.js that:
Keeps track of tasks
Executes callbacks
Handles async operations
Makes Node.js non-blocking
π It decides what code should run next
Single Thread β Single Task β
Node.js:
Has one main thread
But can handle many requests
Because long tasks are handled asynchronously
Thatβs the power of the event loop πͺ
Blocking Code (Bad β)
Blocking code stops everything until it finishes.
Example (Blocking)
const fs = require("fs");
const data = fs.readFileSync("file.txt", "utf-8");
console.log(data);
console.log("This runs last");
π Node.js waits for the file
π No other request can be processed
π Bad for servers β
Non-Blocking Code (Good β )
Example (Non-Blocking)
const fs = require("fs");
fs.readFile("file.txt", "utf-8", (err, data) => {
console.log(data);
});
console.log("This runs first");
π File reading happens in background
π Event loop keeps running
π Server stays fast π
Blocking vs Non-Blocking (Easy Comparison)
| Blocking | Non-Blocking |
| Stops execution | Continues execution |
| Slow servers | Fast servers |
| Simple but risky | Slightly complex but scalable |
How Node.js Handles Multiple Requests π§
Letβs say:
User A β file request
User B β API request
User C β form submission
Node.js:
Accepts all requests
Sends slow tasks to background
Event loop continues running
Responds when tasks are done
π No request waits in a long line
Event Loop Phases (Simplified)
The event loop runs in cycles.
Main Phases (Beginner View)
Timers β
setTimeout,setIntervalI/O Callbacks β file & network
Poll β waits for new tasks
Check β
setImmediateClose callbacks
The loop repeats again and again π
Callbacks Explained Simply π
A callback is:
A function that runs after a task finishes
Example
setTimeout(() => {
console.log("Task completed");
}, 2000);
console.log("Waiting...");
Output
Waiting...
Task completed
π Task goes to background
π Callback runs later
The Problem with Callbacks π΅
Too many callbacks lead to callback hell.
task1(() => {
task2(() => {
task3(() => {
console.log("Done");
});
});
});
β Hard to read
β Hard to maintain
Promises: A Better Way π€
Promises make async code cleaner.
Example
fetchData()
.then(data => processData(data))
.then(result => console.log(result))
.catch(error => console.log(error));
Promise States
| State | Meaning |
| Pending | Still running |
| Fulfilled | Success |
| Rejected | Failed |
Async / Await (Best & Cleanest β¨)
async function getUser() {
try {
const user = await fetchUser();
console.log(user);
} catch (err) {
console.log(err);
}
}
π Looks synchronous
π Works asynchronously
π Uses event loop internally
Blocking vs Non-Blocking Flow Diagram

Blocking Flow
Task A β wait
Task B β wait
Task C

Non-Blocking Flow
Task A β background
Task B β run
Task C β run
Callback when ready
Why the Event Loop Is So Important?
Because it makes Node.js:
Fast
Scalable
Memory-efficient
Perfect for APIs, chat apps, streaming




