If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
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 JavaScript का runtime है जो asynchronous और non-blocking I/O operations को efficiently handle करता है। और इसका core engine है: the Event Loop.
But event loop सिर्फ एक loop नहीं है . It has phases — और हर एक phase का अपना काम होता है।
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 होता है।
●●●
Node.js event loop has six main phases, और ये हर tick/iteration में इस sequence में चलते हैं -
Timers
Pending Callbacks
Idle, Prepare (internal)
Poll
Check
Close Callbacks
चलिए इन सबको समझते हैं with clear examples.
यहां पर execute होती हैं -
setTimeout()
setInterval()
बस ये समझो, जब timer expire होता है, तब callback यहां run होता है।
setTimeout(() => {
console.log('setTimeout callback');
}, 0);यहां पर system-level callbacks जाते हैं, आप directly नहीं लिखते. Example: TCP error retry, some internal operations.
ये internal phases हैं — mostly libuv use करता है इन्हे तो prepare poll phase. Devs को ज़्यादा deal नहीं करना पड़ता।
Poll phase बहुत important है, यहां पर -
I/O operations के results check होते हैं।
अगर poll queue में callback होते हैं, तो वो run होते हैं।
अगर poll empty है, और timers ready नहीं हैं, तो ये phase wait करता है।
यहां run होता है - setImmediate()
setImmediate(() => {
console.log('setImmediate callback');
});setImmediate() तभी run होता है जब poll phase complete हो जाता है।
यहां socket.on('close'), process.on('exit') जैसे close events handle होते हैं।
●●●
ये event loop के बाहर का cycle है ।
ये हमेशा next microtask queue में run होता है — तुरंत, before any phase.
process.nextTick(() => {
console.log('nextTick callback');
});setTimeout(() => {
console.log('setTimeout');
}, 0);
setImmediate(() => {
console.log('setImmediate');
});
process.nextTick(() => {
console.log('nextTick');
});Output
nextTick setTimeout setImmediate
process.nextTick() runs before any phase (microtask).
Then setTimeout() runs in timers phase.
setImmediate() runs in check phase.
●●●
अगर आप Node.js का use production में कर रहे हो — या senior-level interviews दे रहे हो — तो Event Loop का internal working समझना non-negotiable है।
ये आपको help करेगा async code optimize करने में।
और interviews में एक big confidence booster बनेगा।
Loading ...