← Back to Node.js Course | Chapter 4: Async Programming | Lesson 4 of 7

Promise.all का उपयोग

Promise.all एक साथ कई promises चलाता है और इन सबका wait करता है।

In this page:

  1. Promise.all
Syntax
javascript
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

javascript
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. #}
आम गलतियां
  1. independent calls को एक के बाद एक await करना
  2. all की rejection handle न करना
  3. यह मान लेना कि 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:

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.