The Node.js Event Loop

๐ŸŸข Node.js LTS ๐Ÿ“— Chapter 10 of 49 ๐Ÿ“‚ Phase 4: Asynchronous Node.js ๐Ÿ—“๏ธ 2026 Edition
๐Ÿ“Œ Covered in this chapter: Call Stack ยท Event Loop Phases ยท Microtasks vs Macrotasks ยท Non-Blocking I/O

A deep look at how the event loop schedules callbacks, microtasks, and macrotasks to make single-threaded Node.js feel concurrent.

1The Node.js Event Loop โ€” What You'll Learn

A deep look at how the event loop schedules callbacks, microtasks, and macrotasks to make single-threaded Node.js feel concurrent.

Here's everything this chapter covers, in the order you'll learn it:

  • Synchronous vs asynchronous code
  • The call stack
  • The event loop and its phases
  • The callback queue (macrotasks)
  • The microtask queue (Promises)
  • Non-blocking I/O
  • Blocking code and why to avoid it
  • CPU-intensive work and the event loop
  • Common event loop mistakes
  • Debugging asynchronous code
2Working Example
๐Ÿ’ป Example: The Node.js Event Loop
console.log("1: Synchronous");

Promise.resolve().then(() => console.log("3: Microtask (Promise)"));

setTimeout(() => console.log("4: Macrotask (setTimeout)"), 0);

console.log("2: Synchronous");
3Best Practices & Common Pitfalls
๐Ÿ’ก Key things to remember:
  • Microtasks (Promise callbacks, queueMicrotask) always run before the next macrotask (setTimeout, setInterval, I/O callbacks), even with a 0ms delay.
  • Output order above: 1, 2, 3, 4 โ€” synchronous code first, then all pending microtasks, then macrotasks.
โ“ Frequently Asked Questions (FAQ)

Q What's the most important thing to understand about the node.js event loop?

Focus on: Call Stack ยท Event Loop Phases ยท Microtasks vs Macrotasks ยท Non-Blocking I/O. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.

Q Do I need external npm packages for the node.js event loop?

Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ€” otherwise, this chapter relies entirely on Node.js's own built-in capabilities.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on Node.js LTS ยท Last updated August 2026