Back to Blogs
NodeJS
EventLoop
JavaScript
Backend
AsyncProgramming

Understanding the Node.js Event Loop – A Beginner-Friendly Deep Dive

If you’ve ever wondered how Node.js can handle thousands of requests with just one thread and still feel super fast, the secret lies in its Event Loop. Today we’ll understand it step-by-step like a story, with a beautiful animated video and clear code examples – perfect for students!

Admin

Author

11/28/2025

Published

1 views

Views

What is the Event Loop?

The Event Loop is the heart of Node.js. It’s a single-threaded loop that keeps checking: “Are there any pending tasks (like I/O, timers, callbacks)? If yes → execute them. If no → wait.”

Even though Node.js is single-threaded, it feels asynchronous because blocking I/O operations (file reading, database calls, HTTP requests) are offloaded to the operating system using the powerful libuv library.

The Complete Event Loop – Animated Explanation

Here’s the exact flow of the Node.js Event Loop (official animated diagram from Node.js team):


Watch this 30-second animation – it’s the clearest explanation you’ll ever see!


6 Main Phases of the Event Loop

  1. Timers → Executes setTimeout() and setInterval() callbacks that have reached their time.
  2. Pending Callbacks → Executes deferred I/O callbacks (mostly system-level).
  3. Idle, Prepare → Internal Node.js use only.
  4. PollMost important phase! Retrieves new I/O events and executes their callbacks.
  5. Check → Executes setImmediate() callbacks.
  6. Close Callbacks → Handles socket.on('close'), process.exit(), etc.

After all phases → loop repeats forever!

Real Code Examples for Each Phase

console.log("1. Start");

setTimeout(() => {
console.log("2. Timer phase (0ms)");
}, 0);

setImmediate(() => {
console.log("3. Check phase");
});

fs.readFile('./data.txt', 'utf8', (err, data) => {
console.log("4. Poll phase - File read complete");

setTimeout(() => console.log("5. Timer inside Poll"), 0);
setImmediate(() => console.log("6. setImmediate inside Poll"));
});

process.nextTick(() => {
console.log("7. nextTick - runs BEFORE everything!");
});

console.log("8. End of script");

Output:

1. Start
8. End of script
7. nextTick - runs BEFORE everything!
3. Check phase
2. Timer phase (0ms)
4. Poll phase - File read complete
5. Timer inside Poll OR 6. setImmediate inside Poll

Special: process.nextTick() & Microtasks

These run immediately, before any phase!

JavaScript


console.log("Start");

process.nextTick(() => console.log("nextTick"));
queueMicrotask(() => console.log("Microtask"));
Promise.resolve().then(() => console.log("Promise"));

console.log("End");

// Output:
// Start
// End
// nextTick
// Microtask
// Promise

Famous Interview Question


setTimeout(() => console.log("timeout"), 0);
setImmediate(() => console.log("immediate"));

Answer depends on context!

  1. In main file → timeout first
  2. Inside I/O callback → immediate first



Final Words

The Event Loop is not magic — it’s a genius system that makes Node.js perfect for I/O-heavy apps.

Master the Event Loop = Master 80% of Node.js!

Save this post and the video – you’ll thank yourself later! 🚀

Buy Me A Coffee