← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 24 of 26

JS एसिंक पैरेलल

कई independent async operations को एक के बाद एक await करना अनावश्यक रूप से समय बर्बाद करता है, जबकि वे एक साथ भी चल सकते थे -- Promise.all() (और इसके जैसे) आपको कई async operations एक साथ parallel में शुरू करने और सबका मिलकर इंतज़ार करने देते हैं, जो कुल इंतज़ार का समय काफी कम कर देता है।
Syntax
javascript
const results = await Promise.all([promise1, promise2]);
await Promise.allSettled([promise1, promise2]);
await Promise.race([promise1, promise2]);

Sequential Awaiting की समस्या

एक async operation को await करना, फिर अगला शुरू करके उसे await करना, वगैरह, इसका मतलब है कि TOTAL समय हर individual operation के समय का SUM है -- अगर तीन independent operations में से हर एक को एक सेकंड लगता है, तो sequential awaiting में कुल तीन सेकंड लगते हैं, भले ही उनमें से कोई भी असल में एक-दूसरे पर निर्भर नहीं था।

Note: कई operations को एक के बाद एक await करने से पहले, यह पूछें कि क्या उनमें से कोई असल में पिछले वाले के result पर निर्भर करता है -- अगर नहीं, तो वे इसके बजाय parallel में चलने के मजबूत उम्मीदवार हैं।
Warning: ऐसे operations को क्रम में await करना जिनकी एक-दूसरे पर कोई असली dependency नहीं है, async code में एक आम, आसानी से चूक जाने वाली performance गलती है।

उदाहरण: The Problem with Sequential Awaiting

javascript
async function sequential() {
  const a = await new Promise(r => setTimeout(() => r(1), 1000));
  const b = await new Promise(r => setTimeout(() => r(2), 1000));
  console.log(a, b); // takes ~2 seconds total
}
sequential();

Promise.all() से Parallel में चलाना

Promise.all([promise1, promise2, ...]) array में हर promise को तुरंत शुरू कर देता है (वे concurrently चलते हैं, एक के बाद एक नहीं), और उन सबके सफल होने पर, उन्हें पास किए जाने के उसी क्रम में उनके results की एक array के साथ resolve हो जाता है।

Note: पहले हर independent async operation को शुरू करें (हर एक को अलग-अलग await किए बिना), उन्हें एक array में इकट्ठा करें, और उस array को साथ चलाने के लिए Promise.all() को पास करें।
Warning: array में किसी भी एक promise के reject होते ही Promise.all() तुरंत reject हो जाता है, भले ही बाकी अभी pending हों -- आप यह नहीं देख पाते कि बाकी में से कौन सफल हो सकता था।

उदाहरण: Running in Parallel with Promise.all()

javascript
async function parallel() {
  const [a, b] = await Promise.all([
    new Promise(r => setTimeout(() => r(1), 1000)),
    new Promise(r => setTimeout(() => r(2), 1000)),
  ]);
  console.log(a, b); // takes ~1 second total
}
parallel();

Promise.allSettled() से Partial Failures संभालना

Promise.all() के उलट, Promise.allSettled() हमेशा हर promise के खत्म होने का इंतज़ार करता है, चाहे individual success हो या failure -- returned array में हर result में एक status field होती है ("fulfilled" या "rejected") साथ में या तो एक value या एक reason, जो आपको mixed outcomes को अच्छे से संभालने देता है।

Note: Promise.allSettled() का इस्तेमाल खासतौर पर तब करें जब partial failure स्वीकार्य हो और आप हर operation का outcome जानना चाहते हों, न कि पहली rejection पर सब कुछ fail कर देना।
Warning: Promise.allSettled() खुद कभी reject नहीं होता -- group में किसी failure का पता लगाने के लिए हर individual result की status field जांचना ज़रूरी है।

उदाहरण: Handling Partial Failures with Promise.allSettled()

javascript
// Wait for every promise to settle (succeed or fail) before continuing
Promise.allSettled([
  Promise.resolve(1), // resolves immediately with the value 1
  Promise.reject("failed"), // rejects immediately with the reason "failed"
]).then(results => console.log(results)); // results holds a {status, value/reason} object per promise

Promise.race() से Operations को Race कराना

Promise.race([promise1, promise2, ...]) जैसे ही array का पहला promise settle होता है वैसे ही settle हो जाता है (resolve या reject), बाकी आखिरकार क्या करते हैं उसे नज़रअंदाज़ करते हुए -- आमतौर पर timeout लागू करने के लिए इस्तेमाल होता है, किसी असली operation को एक ऐसे promise के खिलाफ race कराना जो एक तय delay के बाद reject होता है।

Note: timeout लागू करने के लिए Promise.race() इस्तेमाल करें: अपने असली operation को एक ऐसे promise के खिलाफ race कराएं जो आपकी चाही गई timeout duration के बाद reject होता है।
Warning: Promise.race() सिर्फ पहले settle हुए promise की रिपोर्ट देता है -- बाकी background में चलते रहते हैं भले ही उनका आखिरी result नज़रअंदाज़ हो जाए, cleanup के मकसद से इसका ध्यान रखना ज़रूरी है।

उदाहरण: Racing Operations with Promise.race()

javascript
// Race two promises; whichever settles first wins, the other is ignored
Promise.race([
  new Promise(r => setTimeout(() => r("data"), 100)), // resolves with "data" after 100ms
  new Promise((_, reject) => setTimeout(() => reject("timeout"), 50)), // rejects with "timeout" after 50ms
]).then(console.log).catch(console.log); // the faster one (the 50ms rejection) wins the race

सही Combinator चुनना

Promise.all() तब सही है जब overall result के मायने रखने के लिए हर operation का सफल होना ज़रूरी हो; Promise.allSettled() तब सही है जब partial success स्वीकार्य हो और आपको हर outcome दिखना चाहिए; Promise.race() "जो भी पहले खत्म हो" वाले scenario के लिए सही है, जैसे timeout।

Note: combinator को अपनी असली failure semantics से मिलाएं: all-or-nothing (all), best-effort (allSettled), या first-to-finish (race)।
Warning: ऐसे scenario के लिए Promise.all() चुनना जहाँ partial success स्वीकार्य होना चाहिए, हर सफल operation के results को फेंक देता है, बस इसलिए कि कोई एक और operation असफल हुआ।

उदाहरण: Choosing the Right Combinator

javascript
Promise.all([Promise.resolve(1), Promise.resolve(2)]).then(r => console.log("all:", r));
Promise.allSettled([Promise.resolve(1), Promise.reject("x")]).then(r => console.log("settled:", r));
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. कई independent promises को अलग-अलग await statements से sequentially await करना, एक के बाद एक, जबकि उन सभी को एक साथ शुरू करने और एक साथ await करने में एक fraction of time लगता।
  2. Promise.all() इस्तेमाल करना जब operations में से कोई fail हो सकता है, यह realize किए बिना कि यह किसी भी promise के reject होते ही तुरंत reject हो जाता है, संभवतः पहले से succeed हुए बाकियों के results खोते हुए।
  3. Promise.all() (किसी भी rejection पर fail fast) को Promise.allSettled() (सब कुछ पूरा होने का इंतज़ार करता है, दोनों successes और failures report करते हुए) से confuse करना।
चैप्टर सारांश
  • Promise.all(arrayOfPromises) कई promises parallel में चलाता है, उन सभी के succeed होने पर resolve होते हुए, या किसी के भी fail होते ही reject होते हुए।
  • Promise.allSettled(arrayOfPromises) भी parallel में चलता है, लेकिन हमेशा हर promise के खत्म होने का इंतज़ार करता है, हर एक की individual success या failure report करते हुए।
  • Promise.race() array में पहले promise के settle होते ही resolve या reject हो जाता है, चाहे वह कोई भी हो।
ब्राउज़र सपोर्ट

Promise.all() को Promises introduce होने के बाद से support मिला है; Promise.allSettled() और Promise.any() सभी current major browsers में supported हैं।

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.