Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

A drop-in replacement for Promise.all() which does not fail-fast.

License

Notifications You must be signed in to change notification settings

raymond-lam/promise-all-reject-later

Repository files navigation

promise-all-reject-later

npm CI Coverage Status MIT license

A drop-in replacement for Promise.all() which does not fail-fast.

Like Promise.all(), promise-all-reject-later returns a single, aggregate Promise that resolves when all of the promises (or any object with a .then() method) in the given iterable argument have resolved or when it is empty. (Anything in the iterable that is not a Promise or does not have a .then() method will be operated upon as if it were wrapped in Promise.resolve().) The resolution value is an Array of the resolution values of the promises in the iterable argument.

However, whereas the Promise returned by Promise.all() rejects as soon as any one of the given promises rejects (the so-called "fail-fast behavior"), if any of the promises given to promise-all-reject-later rejects, the returned Promise will reject only after all of the promises have resolved or rejected. (The rejection reason will be that of the first promise to reject.)

When the aggregate Promise returned by Promise.all() rejects, it loses track of the promises which have not yet resolved or rejected. These promises will still eventually resolve or reject, but the "fail fast" behavior makes it difficult to sequence the execution of code after these pending promises. promise-all-reject-later makes it easy to sequence code after an entire set of promises, whether or not any such promise ends up rejecting.

Usage

const all = require('promise-all-reject-later');

const promise0 = new Promise( ... );
const promise1 = new Promise( ... );
const promise2 = new Promise( ... );

all([
  promise0,
  promise1,
  promise2,
]).catch(
  reason => {
    /*
      If any of the promises reject, this callback will be called after all of
      the promises have resolved or rejected. reason will be the rejection
      reason of the first promise that was rejected.
    */
  }
);

Author

Raymond Lam ([email protected])

License

MIT