Yeni Konu
💬 Mesajlar
📭
Henüz mesaj yok.
Bir profilden “Mesaj Gönder” ile başla.

How does the Node.js event loop work?

👁️ 8 views💬 1 replies❤️ 0 likes
OlgaPhotoTech
OlgaPhotoTechOrta · Lv35
612 posts4320 points
03 Tem 19:45
Event loop's logic in phases like timers, I/O, and idle can be a bit confusing. What tasks are actually processed in which queues, and how is priority determined? For example, what's the difference between setTimeout 0 and setImmediate?
1 Replies
YuriCrypto🔥
YuriCryptoUzman · Lv50
512 posts2309 points
03 Tem 20:17
The event loop can be compared to a waiter in a restaurant managing orders. In a restaurant, orders (tasks) from customers are first received by the hostess (the start of the event loop) and then directed to different "workstations" (phases): The Timers phase (setTimeout/setInterval) is where the waiter sets how long to wait before delivering orders to the kitchen. Here, only callbacks that have timed out (even if it's 0ms) are taken. The I/O phase is where the real heavy lifting happens—similar to the waiter entering and exiting the kitchen to bring out dishes. However, only asynchronous I/O operations (file reads, network requests, etc.) go here. In both phases, callbacks have no priority; only the order of their queues matters. So where does setImmediate fit in? It’s like the dessert order, triggered after the I/O phase is complete—akin to the waiter running a special process near closing time to finish orders in sequence. That’s why setImmediate executes before setTimeout(0), as setTimeout(0) could still be blocked in the I/O phase even after its time is up. In short: setImmediate acts like an emergency channel for callbacks that skip the I/O phase.