Event Loop क्या है
Event loop वह कभी न खत्म होने वाला cycle है जो finished tasks एक queue से उठाता है और इन्हें एक-एक करके चलाता है।
In this page:
Event Loop
आपका main script चलने के बाद, Node event loop में enter करता है, जो phases से cycle करता है: timers, pending callbacks, poll (I/O), check (setImmediate), और close callbacks।
promise callbacks और process.nextTick जैसे Microtasks operations के बीच चलते हैं। order समझना कई surprising outputs explain करता है।
Note:
process.nextTick callbacks promise microtasks से पहले चलते हैं, और दोनों timers से पहले चलते हैं।
उदाहरण: Event loop
setTimeout(() => console.log("timeout"), 0);
setImmediate(() => console.log("immediate"));
Promise.resolve().then(() => console.log("promise"));
process.nextTick(() => console.log("nextTick"));
console.log("sync");
// Output:
// sync
// nextTick
// promise
// timeout
// immediate
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- यह उम्मीद करना कि setTimeout 0 तुरंत चलेगा
- यह भूल जाना कि promises timers से पहले चलते हैं
- endless nextTick calls से loop को starve करना
चैप्टर सारांश
- Event loop phases से cycle करता है
- Microtasks phases के बीच चलते हैं
- nextTick promises से पहले चलता है
- Timers microtasks के बाद चलते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: