Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
⚡ Days 15–22 – Advanced और Async JavaScript  ·  दिन 19

दिन 19: Async JS: Event Loop, Callbacks और Promises

⏱ 100 min📶 Intermediate → Pro🎯 100% self-study
दिन 19 / 30

✅ आज आप सीखेंगे

  • JavaScript को async code क्यों चाहिए
  • Event loop: call stack, task queue, microtasks
  • Timers: setTimeout, setInterval, clearTimeout
  • Callbacks और callback hell क्यों होता है
  • Promises: बनाना, then/catch/finally, Promise.all / allSettled / race / any

🏢 असली दुनिया में

Server से menu load होने में Wi-Fi पर 300 ms और कमज़ोर 4G पर 3 सेकंड लगते हैं। अगर JavaScript इंतज़ार करते हुए कुछ और न करे तो page जम जाएगा – न scroll, न click। Async code browser को network, timer या payment gateway का इंतज़ार करते हुए काम करते रहने देता है। कल का हर API call आज के विचारों पर बनता है।

Synchronous बनाम asynchronous

JavaScript
console.log('1. Take the order');
setTimeout(() => console.log('3. Sweets are ready (after 0 ms!)'), 0);
console.log('2. Take the next customer’s order');
1. Take the order 2. Take the next customer’s order 3. Sweets are ready (after 0 ms!)
🧠 0 ms पर भी 3 आख़िर में क्यों?

JavaScript एक समय में एक काम करता है (single thread)। setTimeout callback को browser/Node को देकर आगे बढ़ जाता है। Callback queue में इंतज़ार करता है जब तक मौजूदा code ख़त्म न हो – तभी event loop उसे चलाता है।

Event loop

Call stack आपका code line-दर-line चलाता हैAsync काम (timers, fetch, clicks) browser को दिया जाता हैतैयार होने पर callbacks queues में इंतज़ार करते हैंपहले microtasks (promise .then), फिर tasks (setTimeout, events)Event loop अगला callback ख़ाली stack पर डालता है
एक thread कई काम कैसे संभालता है
JavaScript
console.log('A: script start');
setTimeout(() => console.log('E: setTimeout (task)'), 0);
Promise.resolve().then(() => console.log('C: promise.then (microtask)'));
queueMicrotask(() => console.log('D: queueMicrotask'));
console.log('B: script end');
A: script start B: script end C: promise.then (microtask) D: queueMicrotask E: setTimeout (task)

Timers

JavaScript
let seconds = 3;
const id = setInterval(() => {
  console.log(`OTP expires in ${seconds}s`);
  seconds--;
  if (seconds === 0) {
    clearInterval(id);                    // always stop intervals
    console.log('OTP expired – request a new one');
  }
}, 100);                                  // 100 ms here so the demo is quick

const reminder = setTimeout(() => console.log('This never prints'), 500);
clearTimeout(reminder);                   // cancelled before it fired
OTP expires in 3s OTP expires in 2s OTP expires in 1s OTP expired – request a new one

Callbacks और callback hell

JavaScript
// Pretend server functions that take time and call us back
function getUser(id, cb)      { setTimeout(() => cb(null, { id, name: 'Aarti' }), 50); }
function getOrders(user, cb)  { setTimeout(() => cb(null, [{ id: 1042, total: 1800 }]), 50); }
function getInvoice(order, cb) { setTimeout(() => cb(null, `Invoice for #${order.id}: ₹${order.total}`), 50); }

getUser(7, (err, user) => {
  if (err) return console.log(err);
  getOrders(user, (err, orders) => {
    if (err) return console.log(err);
    getInvoice(orders[0], (err, invoice) => {      // the "pyramid of doom" grows →
      if (err) return console.log(err);
      console.log(user.name, '→', invoice);
    });
  });
});
Aarti → Invoice for #1042: ₹1800
⚠️ Nested callbacks की समस्या

हर step और गहरा जाता है, error handling हर स्तर पर दोहरानी पड़ती है और steps साथ-साथ चलाना मुश्किल। Promises (आज) और async/await (कल) ठीक यही हल करते हैं।

Promises

JavaScript
// A promise = an object that will hold a future result: pending → fulfilled OR rejected
function bakeGhewar(kg) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (kg > 20) reject(new Error('We can make at most 20 kg today'));
      else resolve(`${kg} kg ghewar ready`);
    }, 100);
  });
}

bakeGhewar(5)
  .then(result => { console.log('✅', result); return bakeGhewar(50); })   // return → chain the next step
  .then(result => console.log('never printed', result))
  .catch(err => console.log('❌', err.message))                            // one catch for the whole chain
  .finally(() => console.log('Kitchen closed for today'));
✅ 5 kg ghewar ready ❌ We can make at most 20 kg today Kitchen closed for today
Stateमतलबकौन संभालता है
pendingअभी चल रहा—
fulfilledValue के साथ सफलता से पूरा.then(value => …)
rejectedError के साथ विफल.catch(err => …)
settled (कोई भी)हो गया, सफल या विफल.finally(() => …)

Callback pyramid, promises से दोबारा

JavaScript
const wait = (ms, value) => new Promise(resolve => setTimeout(() => resolve(value), ms));
const getUser = id => wait(50, { id, name: 'Aarti' });
const getOrders = user => wait(50, [{ id: 1042, total: 1800 }]);
const getInvoice = order => wait(50, `Invoice for #${order.id}: ₹${order.total}`);

let currentUser;
getUser(7)
  .then(user => { currentUser = user; return getOrders(user); })
  .then(orders => getInvoice(orders[0]))
  .then(invoice => console.log(currentUser.name, '→', invoice))
  .catch(err => console.log('Something failed:', err.message));   // flat, one error handler
Aarti → Invoice for #1042: ₹1800

Promises एक साथ चलाना

JavaScript
const wait = (ms, v, fail) => new Promise((res, rej) => setTimeout(() => (fail ? rej(new Error(v)) : res(v)), ms));

const menu = wait(120, 'menu');
const offers = wait(80, 'offers');
const reviews = wait(100, 'reviews');

console.time('all');
Promise.all([menu, offers, reviews]).then(r => { console.log('all:', r); console.timeEnd('all'); });   // ~120 ms, not 300

Promise.allSettled([wait(50, 'menu'), wait(60, 'reviews API down', true)])
  .then(results => console.log('allSettled:', results.map(r => r.status + ':' + (r.value ?? r.reason.message)).join(', ')));

Promise.race([wait(200, 'slow server'), wait(90, 'timeout – show cached menu')])
  .then(winner => console.log('race:', winner));

Promise.any([wait(70, 'mirror 1 down', true), wait(110, 'mirror 2 OK')])
  .then(first => console.log('any:', first));
allSettled: fulfilled:menu, rejected:reviews API down race: timeout – show cached menu any: mirror 2 OK all: [ 'menu', 'offers', 'reviews' ] all: 120.393ms
MethodResolve कबReject कबकब
Promise.allसब सफल (results का array)कोई एक failPage को menu + offers + cart साथ चाहिए
Promise.allSettledसब पूरे (हर एक की status)कभी नहींDashboard widgets जो अलग-अलग fail हो सकते हैं
Promise.raceपहला settle होपहला error से settle होTimeouts
Promise.anyपहला सफल होसब failकई mirrors/CDNs में सबसे तेज़

असली task: “preparing your order” tracker

HTML + JavaScript
<button id="go">Place order</button>
<ol id="steps"></ol>

<script>
const wait = ms => new Promise(r => setTimeout(r, ms));
const steps = document.querySelector('#steps');
function show(text) { const li = document.createElement('li'); li.textContent = text; steps.append(li); }

document.querySelector('#go').addEventListener('click', e => {
  e.target.disabled = true;
  steps.innerHTML = '';
  show('🧾 Order received');
  wait(800)
    .then(() => { show('👩‍🍳 Sweets being packed'); return wait(1000); })
    .then(() => { show('🛵 Out for delivery'); return wait(1200); })
    .then(() => show('✅ Delivered – enjoy!'))
    .finally(() => { e.target.disabled = false; });
});
</script>
▶  A promise chain driving the UI step by step

🏅 आज के Job Skills

✔ Event loop और microtask क्रम समझाना✔ setTimeout, setInterval और उन्हें clear करना✔ Promises बनाना, chain करना और संभालना✔ Promise.all/allSettled/race/any से साथ-साथ काम

Practice tasks

🏋️ Practice – ख़ुद करें

Task 1: timeout(promise, ms) लिखें जो promise के ms से ज़्यादा लेने पर “Request timed out” से reject करे (Promise.race लें)।
Solution देखें
JavaScript
const wait = (ms, v) => new Promise(r => setTimeout(() => r(v), ms));
function timeout(promise, ms) {
  const timer = new Promise((_, reject) => setTimeout(() => reject(new Error('Request timed out')), ms));
  return Promise.race([promise, timer]);
}
timeout(wait(50, 'menu loaded'), 200).then(console.log).catch(e => console.log(e.message));
timeout(wait(500, 'too slow'), 200).then(console.log).catch(e => console.log(e.message));
menu loaded Request timed out
Task 2: Logs के क्रम का अनुमान लगाएँ, फिर चलाएँ।
Solution देखें
JavaScript
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
(async () => { console.log(4); await null; console.log(5); })();
console.log(6);
// expected: 1 4 6 3 5 2
1 4 6 3 5 2
Task 3: “Resend OTP in 30s” button बनाएँ जो disabled रहे और setInterval से गिनती करे, फिर दोबारा active हो।

Quick quiz

🧠 Quick Quiz – ख़ुद को जाँचें

1. Browser में JavaScript है…
एक call stack।
2. पहले क्या चलता है?
Tasks से पहले microtasks।
3. दोहराने वाला timer रोकें:
id रखें।
4. Promise.all reject कब होता है?
हर नतीजे के लिए allSettled।
5. .then के अंदर promise लौटाने से…
यही chaining है।

Interview corner

💼 Interview Corner

Event loop समझाइए।

JavaScript एक call stack पर चलता है। Async operations host (browser/Node) संभालता है; उनके callbacks queues में जाते हैं। Stack ख़ाली होने पर event loop पहले microtask queue ख़ाली करता है (promise reactions, queueMicrotask), फिर अगला task (timers, I/O, events) लेता है, और browsers में tasks के बीच render करता है।

Promise.all बनाम Promise.allSettled?

all सब values के साथ resolve या पहली विफलता पर reject (fail-fast) – जब हर नतीजा ज़रूरी हो। allSettled सबका इंतज़ार करके हर एक की status/value/reason बताता है – जब आंशिक नतीजे उपयोगी हों।

सारांश

  • एक thread + event loop; async काम queues में इंतज़ार करता है।
  • Microtasks (promises) tasks (timers, events) से पहले चलते हैं।
  • Promises: pending → fulfilled/rejected; then/catch/finally; chain के लिए return।
  • Parallel काम और timeouts के लिए all / allSettled / race / any।
➡️ आगे

Day 20: async/await और fetch – loading और error states के साथ API से असली products load करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या setTimeout सटीक है?
नहीं – यह न्यूनतम देरी है। Page व्यस्त हो या tab पीछे हो तो बाद में चल सकता है।
async/await है तो क्या .then सीखना ज़रूरी?
हाँ – async/await promises पर बना है, कई libraries promises लौटाती हैं, और Promise.all/race दोनों शैलियों में इस्तेमाल होते हैं।
← JavaScript (30 Days)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicदिन 20: async/await और fetch: APIs के साथ काम आपकी प्रगति: 19 / 30 (63%) · Lesson 20 / 30 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें