Facebook Pixel

Real-World Event Loop Examples in Node.js

The event loop is best understood with real examples. Here are common patterns you will encounter.

Real-World Event Loop Examples in Node.js

The event loop is best understood with real examples. Here are common patterns you will encounter.

Example 1: Async File Read Does Not Block

When you call fs.readFile, the main thread continues immediately. Other requests are processed while the file reads. When the read completes, the callback runs on the main thread. This is the non-blocking pattern in action.

Example 2: Sync File Read Blocks Everything

If you use fs.readFileSync in a request handler, the event loop is blocked until the file is read. All other requests wait. This is the most common Node.js performance mistake.

Example 3: Promise.all Runs in Parallel

Three independent API calls with await run sequentially, taking 3 times the longest. With Promise.all, they run in parallel, taking only as long as the slowest. The event loop processes all three concurrently.

Example 4: setTimeout(0) Defers to Next Tick

console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4'); outputs 1, 4, 3, 2. Sync code first, then microtasks, then macrotasks. This is the event loop order in action.

Example 5: Worker Thread for CPU-Heavy Work

A heavy computation on the main thread blocks all requests. Moving it to a worker_thread runs it on a separate thread, keeping the main thread's event loop free for other requests.

The Takeaway

Real-world event loop examples show: async I/O does not block, sync APIs do block, Promise.all runs operations in parallel, setTimeout defers to the next tick after microtasks, and worker threads offload CPU-heavy work from the main thread.

fs.readFile does not block: the main thread continues, other requests are processed while the file reads, and the callback runs when it completes. fs.readFileSync blocks: the event loop stops until the file is read. Promise.all runs three API calls in parallel instead of sequentially.

1, 4, 3, 2. Sync code runs first (1, 4), then microtasks (promise callback 3), then macrotasks (setTimeout callback 2). This demonstrates event loop order: sync, then microtasks, then macrotasks.

Three independent API calls awaited sequentially take 3 times the longest. With Promise.all, they run in parallel, taking only as long as the slowest. The event loop processes all three concurrently, demonstrating non-blocking I/O.

A heavy computation on the main thread blocks all requests. Moving it to a worker_thread runs it on a separate thread, keeping the main thread's event loop free for other requests. This shows how CPU-heavy work is offloaded from the event loop.

fs.readFile delegates to libuv and returns immediately, letting the event loop process other requests while the file reads. The callback runs when the read completes. fs.readFileSync blocks the thread until the file is read, stopping all requests.

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.