JS एसिंक पैरेलल
In this page:
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 में कुल तीन सेकंड लगते हैं, भले ही उनमें से कोई भी असल में एक-दूसरे पर निर्भर नहीं था।
उदाहरण: The Problem with Sequential Awaiting
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 हो जाता है।
उदाहरण: Running in Parallel with Promise.all()
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 को अच्छे से संभालने देता है।
उदाहरण: Handling Partial Failures with Promise.allSettled()
// 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 होता है।
उदाहरण: Racing Operations with Promise.race()
// 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।
उदाहरण: Choosing the Right Combinator
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));
- कई independent promises को अलग-अलग await statements से sequentially await करना, एक के बाद एक, जबकि उन सभी को एक साथ शुरू करने और एक साथ await करने में एक fraction of time लगता।
- Promise.all() इस्तेमाल करना जब operations में से कोई fail हो सकता है, यह realize किए बिना कि यह किसी भी promise के reject होते ही तुरंत reject हो जाता है, संभवतः पहले से succeed हुए बाकियों के results खोते हुए।
- 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 हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic