🔄
Advanced JavaScript

The Event Loop Deep Dive

Call Stack, Task Queue, Microtasks
💡 Event loop ek restaurant manager jaisa hai — call stack current orders hai jo chef (JavaScript engine) abhi bana raha hai (ek time par ek). Jab ek order "async" hota hai (kitchen se bahar kisi supplier ko bhejna), manager use side mein rakhta hai, aur baaki orders continue karta hai. Jab supplier wapas aata hai, manager use queue mein daal deta hai, jab chef free ho.

JavaScript ka execution model: Call Stack (jaha current synchronous code chalता hai), Web APIs (browser ki async capabilities — setTimeout, fetch), Callback Queue/Task Queue (completed async operations ke callbacks yaha wait karte hain), Microtask Queue (Promises ke liye, HIGHER priority queue).

Event loop continuously check karta hai: "kya call stack khaali hai?" Agar haan, microtask queue se saara kaam pehle karo, PHIR task queue se ek kaam lo. Ye wajah hai ki Promises (microtasks) setTimeout (macrotask) se PEHLE chalte hain, chahe dono "ready" ho.

console.log("1: Sync");

setTimeout(() => console.log("2: setTimeout (macrotask)"), 0);

Promise.resolve().then(() => console.log("3: Promise (microtask)"));

console.log("4: Sync");

// Output order: 1, 4, 3, 2
// Sync code PEHLE (1, 4), phir microtasks (3 - Promise),
// PHIR macrotasks (2 - setTimeout) — chahe setTimeout ka delay 0 ho!
🔄
Event loop ek restaurant manager jaisa hai — call stack current orders hai jo chef (JavaScript engine) abhi bana raha hai (ek time par ek). Jab ek order "async" hota hai (kitchen se bahar kisi supplier ko bhejna), manager use side mein rakhta hai, aur baaki orders continue karta hai. Jab supplier wapas aata hai, manager use queue mein daal deta hai, jab chef free ho.
1 / 6
⚡ झट से Recap
  • Call Stack = current sync execution, Web APIs = async operations handle karte hain
  • Microtask Queue (Promises) = HIGHER priority than Task Queue (setTimeout)
  • Event loop: stack khaali → microtasks saare → ek macrotask → repeat
इस page में (2 subtopics)

requestAnimationFrame() ek special browser API hai jo callback ko browser ke NEXT REPAINT se pehle chalata hai — setTimeout() se better hai smooth animations ke liye, kyunki browser ke refresh rate se sync rehta hai.

function animate() {
  // position update logic yaha
  console.log("Frame render ho raha hai");
  requestAnimationFrame(animate);   // agla frame schedule karo
}

requestAnimationFrame(animate);   // animation loop shuru
💡Tip: Animations ke liye setTimeout() ki jagah requestAnimationFrame() prefer karo — browser ke rendering cycle se sync hota hai (usually 60fps), smoother results deta hai, aur tab background mein hone par automatically pause ho jaata hai (battery/performance friendly).

Node.js mein process.nextTick() microtask queue se bhi PEHLE chalta hai (highest priority) — browser JavaScript mein directly available nahi hai, Node.js-specific concept, lekin event loop priority samajhne ke liye relevant hai.

// Node.js mein priority order (highest to lowest):
// 1. process.nextTick() callbacks
// 2. Promise microtasks
// 3. setTimeout/setInterval (macrotasks)

console.log("1: Sync");
setTimeout(() => console.log("4: setTimeout"), 0);
Promise.resolve().then(() => console.log("3: Promise"));
process.nextTick(() => console.log("2: nextTick"));
// Output: 1, 2, 3, 4
💡Tip: process.nextTick() sirf Node.js mein hai (browser mein nahi) — lekin concept samajhna helpful hai "priority levels" ke overall mental model ke liye, especially agar Node.js backend bhi likhoge kabhi.