Suggest an editImprove this articleRefine the answer for “What does Promise.all() do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Promise.all()`** is a method of the `Promise` class that takes an array (or any iterable) of promises and returns a new promise that fulfills once all the promises inside the array have succeeded, or rejects as soon as the first promise fails. **Key point:** on success the result is an array of values in the same order as the input promises, and the first rejection instantly rejects the whole `Promise.all()`.Shown above the full answer for quick recall.Answer (EN)Image## What `Promise.all()` does `Promise.all()` is a **method of the `Promise` class** that takes an array (or any iterable) of promises and **returns a new promise** that: - **fulfills** once **all the promises** inside the array have succeeded (`fulfilled`); - **rejects**, as soon as the **first promise** fails. --- ## Syntax ```javascript Promise.all(iterable) ``` where `iterable` is usually an array of promises: ```javascript const p1 = Promise.resolve('A'); const p2 = Promise.resolve('B'); const p3 = Promise.resolve('C'); Promise.all([p1, p2, p3]).then(values => console.log(values)); ``` **Result:** ```javascript ["A", "B", "C"] ``` --- ## How it works 1. All the promises inside `Promise.all([...])` **start in parallel**. (more precisely, they begin executing *independently of each other*). 2. `Promise.all()` **waits** for them to finish. 3. If all succeed, a new promise in the `fulfilled` state is returned, and its value is an **array of results in the same order** as the input promises. --- ## Example with delays ```javascript const p1 = new Promise(res => setTimeout(() => res('First'), 1000)); const p2 = new Promise(res => setTimeout(() => res('Second'), 2000)); const p3 = new Promise(res => setTimeout(() => res('Third'), 500)); Promise.all([p1, p2, p3]) .then(results => console.log('Results:', results)) .catch(err => console.error('Error:', err)); ``` Output after about 2 seconds: ```javascript Results: [ 'First', 'Second', 'Third' ] ``` Notice: the results are **in the same order** the promises were passed in, not in the order they finished. --- ## What happens if one of the promises rejects If **at least one** promise inside `Promise.all` transitions to the **rejected** state, the whole `Promise.all()` **immediately rejects**, and the result is the **first caught error**. ### Example: ```javascript const p1 = Promise.resolve('OK'); const p2 = Promise.reject('Error!'); const p3 = Promise.resolve('Also OK'); Promise.all([p1, p2, p3]) .then(results => console.log(results)) .catch(err => console.error(err)); ``` Output: ```javascript Error! ``` The rest of the promises (`p3`) still run, but their **results are ignored**, because `Promise.all()` has already transitioned to the **rejected** state. --- ## Important: results are kept in the order they were passed Even if one promise takes longer to resolve, the results are ordered **by position**: ```javascript Promise.all([ Promise.resolve('A'), new Promise(res => setTimeout(() => res('B'), 2000)), Promise.resolve('C') ]).then(console.log); // ["A", "B", "C"] ``` --- ## If you pass non-promises `Promise.all()` automatically wraps plain values in `Promise.resolve()`: ```javascript Promise.all([42, Promise.resolve('OK')]) .then(console.log); // [42, "OK"] ``` --- ## How to handle errors without interrupting execution If you need **all promises to run independently**, and errors **should not interrupt** execution, use `Promise.allSettled()`. ```javascript Promise.allSettled([ Promise.resolve('OK'), Promise.reject('Error'), Promise.resolve('Also OK') ]).then(console.log); ``` Output: ```javascript [ { status: "fulfilled", value: "OK" }, { status: "rejected", reason: "Error" }, { status: "fulfilled", value: "Also OK" } ] ``` `Promise.allSettled()` **never rejects**, and returns an array of results with `status` and `value`/`reason` fields. --- ## A simple analogy > Imagine you're waiting for 3 deliveries: > > - `Promise.all()`: you wait for **all the packages at once**. > If even one gets **lost**, you **cancel everything**. > - `Promise.allSettled()`: you wait for **all of them**, > even if one gets **broken**, you still get a report on each. --- ## SUMMARY | Method | What it does | Success | Error | |---|---|---|---| | `Promise.all()` | Waits for all promises to fulfill | Returns an array of results | Rejects on the first error | | `Promise.allSettled()` | Waits for all, even with errors | Returns an array of `{status, value/reason}` objects | - | | `Promise.race()` | Returns the result of the **first promise to settle** | Can be a success or an error | - |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.