A Promise
can either resolve into a result or reject into an error. In this blog we will see how we can wait for multiple promises, depending on our requirements and use cases. We will cover Promise.all
, Promise.allSettled
, Promise.race
and Promise.any
functions in particular. We will see example snippets and analyze the output for better understanding.
Let’s define the common functions which will be used in our example snippets. As these functions are common, we will define them only once, to avoid repetition. These will be used in all the later snippets. resolveAfter
returns a promise which resolves after the timeout (in seconds), which is passed as the argument. rejectAfter
returns a promise which rejects after the timeout (in seconds), which is passed as the argument.
Promise.all
Promise.all
function takes in an array of promises as the argument. It returns a promise, which gets resolved or rejected as per the following:
- It resolves, if all the promises in the Array are resolved, i.e. it waits for all the promises to get resolved.
- It rejects, as soon as any one promise in the Array is rejected.
The return value is an Array of the results from the promises in the same order as the promise Array.
Now let’s see some examples along with their output.
Example 1 - All three promises resolved
Following is the output. Promise.all
waits for all the promises to get resolved.
Example 2 - Two promises resolved, one rejected
Following is the output. Promise.all
rejects as soon as the first reject is obtained. It does not wait for the third promise to get settled (resolved or rejected).
Promise.allSettled
Promise.allSettled
function takes in an array of promises as the argument. It returns a promise, which gets resolved after each of the input promises is settled (resolved or rejected).
The return value is an array of objects with keys - status
and value
. status
can be fulfilled or rejected. value
is result of resolution or error of rejection. The order of the objects in the result array corresponds to the order of the input promise array.
Now let’s see some examples along with their output.
Example 1 - All three promises resolved
Following is the output. Promise.allSettled
resolves after all the input promises are settled (resolved in this case).
Example 2 - Two promises resolved, one rejected
Following is the output. Promise.allSettled
resolves after all the input promises are settled (two resolved and one rejected in this case).
Promise.race
Promise.race
function takes in an array of promises as the argument. It returns a promise, which gets resolved or rejected as soon as the first promise in the input promise array is resolved or rejected.
The return value is the same as the resolved value of the first resolved promise. In the case of rejection, the reason for the rejection is the same as that given by the first rejected promise.
Now let’s see some examples along with their output.
Example 1 - All three promises resolved
Following is the output. Promise.race
resolves after the first promise resolves.
Example 2 - Two promises resolved, one rejected (Time-wise first one resolved)
Following is the output. Promise.race
resolves after the first promise resolves.
Example 3 - Two promises resolved, one rejected (Time-wise first one rejected)
Following is the output. Promise.race
rejects after the first promise rejects.
Promise.any
Promise.any function takes in an array of promises as the argument. It returns a promise, which gets resolved at the first resolution amongst the promise array. If no promise resolves, then Promise.any rejects.
So it waits for any of the promises to get resolved. If it does not find any resolutions, then it rejects.
In the case of resolution, the resolved value is the same as the resolved value of the first resolved promise.
Now let’s see some examples along with their output.
Example 1 - Time-wise second promise resolved. Others rejected.
Following is the output. Promise.any
resolves after the second promise resolves.
Example 2 - All promises rejected.
Following is the output. Promise.any
resolves after all the promises reject.