Node.js Event Loop Internals Explained | Phases, Execution Order, and Examples

Other Blogs

Blogs ❯❯ NodeJS

Image could not load

Event Loop Phases by Favour Usifo on Unsplash

अगर आप Node.js backend developer हो — especially किसी senior-level interview के preparation mode में — तो एक concept आपको 100% crystal clear होना चाहिए।

Node.js Event Loop and its internal phases

बहुत log event loop का overview जानते हैं, पर जब बात आती है -

“setTimeout, setImmediate, और process.nextTick() का actual execution order क्या होता है?”

“Poll phase क्या करता है? Close callbacks क्या होते हैं?”

तब confusion हो जाता है।

Node.js : A Event-Driven Platform

Node.js JavaScript का runtime है जो asynchronous और non-blocking I/O operations को efficiently handle करता है। और इसका core engine है: the Event Loop.

But event loop सिर्फ एक loop नहीं है . It has phases — और हर एक phase का अपना काम होता है।

What is Event Loop?

Event loop एक mechanism है जो JavaScript code, callbacks, timers, and I/O events को sequence में execute करता है — single-threaded model में, लेकिन highly efficient तरीके से।

जब आप async काम लिखते हो (जैसे fs.readFile() या setTimeout()), वो Node.js के libuv layer में handle होता है। एक बार काम हो गया फिर callback event loop के phases में queue होता है।

Event Loop Phases

Node.js event loop has six main phases, और ये हर tick/iteration में इस sequence में चलते हैं -

  1. Timers

  2. Pending Callbacks

  3. Idle, Prepare (internal)

  4. Poll

  5. Check

  6. Close Callbacks

चलिए इन सबको समझते हैं with clear examples.

1. Timers Phase

यहां पर execute होती हैं -

  • setTimeout()

  • setInterval()

बस ये समझो, जब timer expire होता है, तब callback यहां run होता है।

setTimeout(() => { console.log('setTimeout callback'); }, 0);

2. Pending Callbacks

यहां पर system-level callbacks जाते हैं, आप directly नहीं लिखते. Example: TCP error retry, some internal operations.

3. Idle & Prepare

ये internal phases हैं — mostly libuv use करता है इन्हे तो prepare poll phase. Devs को ज़्यादा deal नहीं करना पड़ता।

4. Poll Phase

Poll phase बहुत important है, यहां पर -

  • I/O operations के results check होते हैं।

  • अगर poll queue में callback होते हैं, तो वो run होते हैं। 

  • अगर poll empty है, और timers ready नहीं हैं, तो ये phase wait करता है।

5. Check Phase

यहां run होता है - setImmediate()

setImmediate(() => { console.log('setImmediate callback'); });

setImmediate() तभी run होता है जब poll phase complete हो जाता है।

6. Close Callbacks

यहां socket.on('close'), process.on('exit') जैसे close events handle होते हैं।

process.nextTick() vs setTimeout vs setImmediate

 process.nextTick()
  • ये event loop के बाहर का cycle है ।

  • ये हमेशा next microtask queue में run होता है — तुरंत, before any phase.

process.nextTick(() => { console.log('nextTick callback'); });

Execution Order Demo

setTimeout(() => { console.log('setTimeout'); }, 0); setImmediate(() => { console.log('setImmediate'); }); process.nextTick(() => { console.log('nextTick'); });

Output

nextTick
setTimeout
setImmediate
Why?
  • process.nextTick() runs before any phase (microtask).

  • Then setTimeout() runs in timers phase.

  • setImmediate() runs in check phase.

Conclusion

अगर आप Node.js का use production में कर रहे हो — या senior-level interviews दे रहे हो — तो Event Loop का internal working समझना non-negotiable है।

  • ये आपको help करेगा async code optimize करने में।

  • और interviews में एक big confidence booster बनेगा।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !