NODEJS Contents

Event Loop Basics (What Actually Runs When)

Understand the basic cycle of the Node.js event loop and how tasks move through its execution phases.

On this page

The Loop Concept

The event loop continuously checks for pending tasks and executes them when the call stack is empty.

Phases Overview

  • Timers
  • Pending Callbacks
  • Poll
  • Check
  • Close Callbacks

Timer Example

setTimeout(() => console.log('timer'), 0);
console.log('sync');

Execution Order

Synchronous code runs first. Then queued callbacks execute according to loop phase rules.

Production Rule

Keep synchronous sections small to allow the loop to progress smoothly.