Wait until all promises complete even if some rejected
When dealing with multiple promises in JavaScript and you need to wait for all of them to complete, irrespective of whether they passed or failed, we use Promise.allSettled()
. This function bundles all your promises into a single one that resolves with an array showing the outcome of each promise.
The response for each outcome includes the status
('fulfilled' or 'rejected'), the value
(if fulfilled), or reason
(if rejected).
Diving deep with Promise.allSettled()
In this section, we extend beyond just using Promise.allSettled
, dive deeper into handling the outcomes, and cater to environments where it might not be available yet.
Postprocessing the results of allSettled
Arguably, after you've received the results from Promise.allSettled()
, you will want to separate the successful operations from the faltered. You can achieve this by filtering the outcomes based on the status
property.
Fulfilling promises in ES5
Promise.allSettled
may not exist in environments that don't support ES2020 or later. However, you can circumvent this by using a polyfill or create a custom settle
method to mimic Promise.allSettled
behavior.
With this polyfill, older environments can also use Promise.allSettled
to ensure all promises are awaited.
Nailing error handling
Another pattern beyond Promise.allSettled
is to catch errors for each promise individually using a reflect
function. This way, you ensure no promise is left unattended and each can resolve or reject gracefully to aggregate the results.
Beyond waiting - handling outcomes
Just waiting for all promises to settle won't cut it. We need to adequately manage the outcomes for each promise. So, let's dig a bit deeper and learn how we can harness the detailed information provided by Promise.allSettled
.
Enforcing a uniform promise interface
By insisting that each promise conforms to a consistent interface (i.e., an object with a status
and either value
or reason
), we can conveniently abstract the error handling and process the results predictably.
Keeping out external libraries
The promise management features in modern JavaScript environments have considerably reduced the reliance on external promise libraries. Since Promise.allSettled
is natively available, it often negates the need for additional dependencies for promise management in your projects.
Simple over complex error handling
Sometimes, simpler is better, aligning nicely with the KISS (Keep It Simple, Stupid) principle. Instead of developing complex error handlers, defaulting to a base case for errors can often suffice. In essence, if something ain't broke, don't complicate it.
Handling network failures elegantly
When dealing with network requests being graceful in failure management is vital. Possible scenarios you need to factor in may include slow network connections, request timeouts, and server issues - all unpredictable and fun parts of any developer's life.
Was this article helpful?