Promise.all का उपयोग
Promise.all एक साथ कई promises चलाता है और इन सबका wait करता है।
In this page:
Syntax
const results = await Promise.all([promise1, promise2, promise3]);
Promise.all
Promise.all promises का एक iterable लेता है और order में results के एक array से resolve होता है, या एक के reject होते ही reject हो जाता है। Promise.allSettled सबका wait करता है और हर outcome report करता है, Promise.race पहले settle होने वाले से resolve होता है और Promise.any पहली success से।
Note:
जब एक failure को बाकी cancel नहीं करना चाहिए तो allSettled इस्तेमाल करें।
उदाहरण: Promise.all
const wait = (ms, v, fail) => new Promise((res, rej) => setTimeout(() => (fail ? rej(new Error(v)) : res(v)), ms));
(async () => {
console.log(await Promise.all([wait(20, "a"), wait(5, "b"), wait(10, "c")]));
const r = await Promise.allSettled([wait(5, "ok"), wait(5, "bad", true)]);
console.log(r.map((x) => x.status));
console.log(await Promise.race([wait(20, "slow"), wait(5, "fast")]));
})();
// Output:
// [ 'a', 'b', 'c' ]
// [ 'fulfilled', 'rejected' ]
// fast
⚠️ Run this in your own terminal or Node.js environment.
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. #}
आम गलतियां
- independent calls को एक के बाद एक await करना
- all की rejection handle न करना
- यह मान लेना कि results completion order में आती हैं
चैप्टर सारांश
- all ordered results से resolve होता है
- यह पहली failure पर reject होता है
- allSettled हर outcome report करता है
- race और any एक winner चुनते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: