Facebook Pixel

libuv Event Loop Phases Explained: Timers, Poll, Check, and Close

The event loop has phases. Here is each phase explained, from timers to close callbacks.

libuv Event Loop Phases Explained: Timers, Poll, Check, and Close

The libuv event loop has phases. Understanding them is key to understanding Node.js's async behavior.

Phase 1: Timers

Runs callbacks scheduled by setTimeout and setInterval. The event loop checks if any timer's threshold has been reached, and runs those callbacks. This is the first phase of each loop iteration.

Phase 2: Pending Callbacks

Executes I/O callbacks deferred to the next loop iteration. Some callbacks like TCP errors are deferred here. This is a less-known phase but handles deferred system-level callbacks.

Phase 3: Idle, Prepare

Internal phases used by libuv internally. You do not interact with these as a developer, but they are part of the loop's structure for internal bookkeeping.

Phase 4: Poll

Retrieves new I/O events and executes I/O-related callbacks. If there are no timers scheduled and no pending callbacks, the event loop blocks here waiting for events. This is where most I/O callbacks run.

Phase 5: Check

Executes callbacks scheduled by setImmediate. setImmediate runs after the poll phase, making it useful for running code right after I/O events are processed.

Phase 6: Close Callbacks

Executes close callbacks, like when a socket or file is closed. These are internal cleanup callbacks for resources that are shutting down.

The Takeaway

The libuv event loop has phases: timers (setTimeout), pending callbacks, idle/prepare (internal), poll (I/O callbacks), check (setImmediate), and close callbacks. Understanding these phases explains the order in which async callbacks run.

Timers (setTimeout and setInterval callbacks), pending callbacks (deferred I/O callbacks), idle/prepare (internal), poll (new I/O events and I/O callbacks), check (setImmediate callbacks), and close callbacks (cleanup for closed resources).

Callbacks scheduled by setTimeout and setInterval run. The event loop checks if any timer's threshold has been reached and runs those callbacks. This is the first phase of each loop iteration.

The event loop retrieves new I/O events and executes I/O-related callbacks. If there are no timers scheduled and no pending callbacks, it blocks here waiting for events. This is where most I/O callbacks run.

Callbacks scheduled by setImmediate run. setImmediate runs after the poll phase, making it useful for running code right after I/O events are processed.

Because it explains the order in which async callbacks run. Knowing that timers run before I/O callbacks, and setImmediate runs after I/O, helps you reason about and debug async behavior in Node.js, which is essential for backend development.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.