Using Promise.allSettled


Promise.allSettled can be a powerful mechanism for handling sets of promises.

Promise.allSettled will resolve when all the promises passed as an argument have fulfilled, whether they have resolved or rejected.

This allows you to define behavior for dealing with partial successes and failures when retrieving multiple resources from various locations. A common use case might be downloading and/or uploading several files to a remote server.

Simply using an await Promise.allSettled(promises) will always resolve, as the promise returned by this API does not care whether the passed promises are resolved or rejected.

The Promise.allSettled resolution object will be an array of promise resolutions/rejections. We can await the Promise.allSettled statement and then iterate through the results, conditionally handling whether each promise was rejected or not.

We need to check each promise status individually:

const response = await Promise.allSettled(promises);
response.forEach((response) => {
      if (response.status === 'rejected') {
        // Handle your rejection
      } else {
        // Handle success
Handling each response in Promise.allSettled