← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 7 of 26

JS इवेंट लूप

Event loop वह तरीका है जिससे JavaScript एक साथ कई काम संभालता है भले ही वह एक बार में एक ही चीज़ करता हो, जैसे एक अकेला waiter जो एक queue से orders लेता है। यह अगला काम शुरू करने से पहले हमेशा current काम खत्म करता है।

Event Loop क्या है?

Event loop JavaScript को asynchronous काम संभालने में मदद करता है जबकि main thread उपलब्ध रहती है।

यह JavaScript को एक साथ single-threaded और non-blocking दोनों रहने देता है — लंबे समय तक चलने वाला I/O page को freeze नहीं करता, क्योंकि उसे main synchronous flow के बाहर संभाला जाता है।

उदाहरण: What Is the Event Loop?

javascript
console.log("1");
setTimeout(() => console.log("2"), 0);
console.log("3"); // 1, 3, 2 - main thread finishes first

Call Stack

JavaScript synchronous function calls को call stack पर चलाता है। call stack के पूरी तरह खाली होने पर ही event loop किसी queue से अगला task निकालकर उसे चलाने के लिए stack पर push करता है।

उदाहरण: Call Stack

javascript
// Define the function `a` with no parameters
// Define the function `a` with no parameters
function a() { b(); }
// Define the function `b` with no parameters
// Define the function `b` with no parameters
function b() { console.log("on the call stack"); }
// Call `a()`
// Call `a()`
a();

Task Queue

Timer callbacks और कई browser callbacks event loop के उन्हें चलाने से पहले queues में इंतज़ार करते हैं। ये queued callbacks तुरंत नहीं चलते, 0ms delay के साथ भी नहीं — वे पहले current synchronous code के खत्म होने और call stack के साफ होने का इंतज़ार करते हैं।

उदाहरण: Task Queue

javascript
console.log("start");
setTimeout(() => console.log("from queue"), 0);
console.log("end"); // queued callback waits for the stack to clear

Microtasks

Promise callbacks microtasks होते हैं। ये आमतौर पर current code खत्म होने के बाद timer callbacks से पहले चलते हैं।

चूंकि अगले task queue item के चलने से पहले microtasks को पूरी तरह drain किया जाता है, resolved Promises की एक chain एक setTimeout(fn, 0) callback से पहले चल सकती है, एक tick बाद भी।

उदाहरण: Microtasks

javascript
// Print "1" to the console
// Print "1" to the console
console.log("1");
// Call `Promise.resolve().then(() => console.log("2 - microtask"))`
// Call `Promise.resolve().then(() => console.log("2 - microtask"))`
Promise.resolve().then(() => console.log("2 - microtask"));
// Call `setTimeout(() => console.log("3 - timer"), 0)`
// Call `setTimeout(() => console.log("3 - timer"), 0)`
setTimeout(() => console.log("3 - timer"), 0);
// Print "4" to the console
// Print "4" to the console
console.log("4");

Practical क्रम

execution order समझना आपको asynchronous JavaScript के output का अंदाज़ा लगाने में मदद करता है। यह क्रम उस output को समझाता है जो पहली नज़र में हैरान करने वाला लगता है, जैसे code में पहले schedule किए गए किसी setTimeout से पहले एक Promise handler का log होना।

उदाहरण: Practical Order

javascript
console.log("A");
setTimeout(() => console.log("B - timer"), 0);
Promise.resolve().then(() => console.log("C - microtask"));
console.log("D");
// Order: A, D, C, B
Live Example
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(fn, 0) तुरंत चलता है, जबकि यह queue होता है और current code और microtasks के बाद चलता है।
  2. एक लंबा loop चलाना जो call stack को block कर देता है, इसलिए clicks और timers नहीं चल पाते।
  3. यह उम्मीद करना कि promise callbacks timers के बाद चलेंगे, जबकि microtasks अगले timer task से पहले चलते हैं।

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.