← Back to Node.js Course | Chapter 1: Introduction | Lesson 3 of 7

Event Loop क्या है

Event loop वह कभी न खत्म होने वाला cycle है जो finished tasks एक queue से उठाता है और इन्हें एक-एक करके चलाता है।

In this page:

  1. Event Loop

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

javascript
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. #}
आम गलतियां
  1. यह उम्मीद करना कि setTimeout 0 तुरंत चलेगा
  2. यह भूल जाना कि promises timers से पहले चलते हैं
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.