# Promises in parallel

[JS](../..) ⟩ [async](..) ⟩ [Promise]() ⟩ in parallel

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %}
[`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)&#x20;

* will <mark style="color:red;">**reject immediately**</mark> upon <mark style="color:yellow;">**any**</mark> of the input promises rejecting.
{% endhint %}

{% hint style="success" %}
[`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)  ([es2020](../../feature/es2020 "mention"))

* will wait for <mark style="color:yellow;">**all input promises**</mark> to <mark style="color:orange;">**complete**</mark>, <mark style="color:red;">**regardless of whether or not one rejects**</mark>.&#x20;
* will <mark style="color:yellow;">**always**</mark> return the <mark style="color:yellow;">**final result**</mark> of <mark style="color:yellow;">**every**</mark> promise and function from the input iterable. (**order is preserved**)
{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}
* [promises](../await/promises "mention") (in parallel or in series)
{% endtab %}

{% tab title="💈範例" %}
```javascript
// rejected if any of the input Promises are rejected,
// otherwise, fulfilled with an array of values of each Promise.
Promise.all(
    // array of URLs -> array of Promises
    urls.map(url => fetch(url).then(response => response.text())
).then(
    texts => handle(texts)    // do something with array of strings
)
```



:floppy\_disk: replit：[Promise.all()](https://replit.com/@pegasusroe/Promiseall#index.js)

```javascript
// custom Promise
const promiseFoo = new Promise((resolve, reject) => {
    setTimeout(resolve, 2000, 'foo');
    //                        ╰───╯ <---- parameter to `resolve`
});

// ⭐ Promise.all()
// - rejected if any is rejected
// - fulfilled (with array of values) if all are fulfilled
Promise.all([
    Promise.resolve(3),
    42,                     // non-Promise value is OK
    promiseFoo
]).then(values => {
    console.log(values);    // [ 3, 42, 'foo' ]
});

// ⭐ Promise.allSettled()
// - always fulfilled with array of outcomes (objects)
Promise.allSettled([
    Promise.resolve(1),
    Promise.reject('bad boy'),
    3,
]).then(results => {
    results.forEach(result => console.log(result));
    // { status: 'fulfilled',  value: 1         }    // ⭐ with `value`
    // { status: 'rejected' , reason: 'bad boy' }    // ⭐ with `reason`
    // { status: 'fulfilled',  value: 3         }
});
```



:floppy\_disk: replit：[Promise.race()](https://replit.com/@pegasusroe/Promiserace#index.js)

```javascript
// ⭐ Promise.race()
// - settled immediately if any is settled.
Promise.race([
    new Promise(resolve => setTimeout(resolve, 500, 'one')),
    new Promise(resolve => setTimeout(resolve, 200, 'two')),    // resolve faster
]).then(value => {
    console.log(value);    // 'two'
});
```
{% endtab %}

{% tab title="📘 手冊" %}
* [Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) - rejected <mark style="color:red;">**immediately**</mark> if any rejetcted, fulfilled if **all fulfilled**.
* [Promise.allSettled()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) - a [](<> "mention") that will <mark style="color:yellow;">**always**</mark> be <mark style="color:green;">**fulfilled**</mark> (with an array of outcome objects) when **all settled**.
* [Promise.race()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) - settled <mark style="color:red;">**immediately**</mark> if any is settled.
{% endtab %}

{% tab title="👥 相關" %}
* [promises](../await/promises "mention")
{% endtab %}
{% endtabs %}
