Typorithm · hard

Event Loop Phases

node-event-loop-phasesnodejsevent-looplibuvphases
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Event Loop Phases

0%

Event Loop

libuv's event loop cycles through fixed phases in order: timers, pending callbacks, poll, check, then close callbacks. The poll phase is where the loop waits for I/O. Each phase fully drains its own callback queue before moving on, and non-blocking I/O is what lets one thread stay concurrent.

Node's event loop, implemented by libuv, repeats a fixed sequence of phases: timers, pending callbacks, poll, check, and finally close callbacks, then loops back to the start.

The poll phase is the heart of the loop — it is where Node waits for and retrieves incoming I/O events and runs their callbacks, blocking there until work arrives or a timer is due.

Each phase has its own callback queue and fully drains every ready callback in it before advancing to the next phase; because I/O is non-blocking, a single thread can juggle many connections concurrently.

01

Order: timers -> pending callbacks -> poll -> check -> close callbacks.

02

The poll phase waits for I/O and runs its callbacks.

03

Each phase drains its own queue before the next runs.

// libuv runs these phases in a fixed order, looping forever
// 1. timers          -> setTimeout / setInterval callbacks
// 2. pending callbacks-> deferred system (TCP error) callbacks
// 3. poll            -> wait for I/O, run its callbacks
// 4. check           -> setImmediate callbacks
// 5. close callbacks -> 'close' events, e.g. socket.on('close')
// each phase fully drains its own callback queue before moving on

Read the implementation

Connect each part of the code to the concept before you type.

1. Timers phase

The timers phase runs callbacks scheduled by setTimeout and setInterval whose threshold has elapsed.

// 1. timers          -> setTimeout / setInterval callbacks

2. Poll phase

The poll phase is where the loop waits for and processes I/O events; it can block here until work or a timer is due.

// 3. poll            -> wait for I/O, run its callbacks

3. Check phase

The check phase runs setImmediate callbacks, which is the phase immediately after poll.

// 4. check           -> setImmediate callbacks

4. Close callbacks

The close callbacks phase fires 'close' events like socket.on('close') before the loop wraps around.

// 5. close callbacks -> 'close' events, e.g. socket.on('close')

5. Queue draining

Each phase has its own FIFO queue and runs every ready callback in it before advancing to the next phase.

// each phase fully drains its own callback queue before moving on

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Event Loop Phases: Lesson & Practice · Typorithm | ApplyDjinn