blend / promise-utils

Lodash-like, dependency-free utilities for native ES6 promises.
MIT License
282 stars 15 forks source link

Preserve order of settleAll results if error occurs #53

Open evenfrost opened 4 years ago

evenfrost commented 4 years ago

Hi,

Current behavior of settleAll function is that if some promise throws an error, it is removed from the results array and added to errors array. This way the order of promises is broken and additionally, it's hard to guess, from which promise the errors comes. E.g.

  /* As an example, secondPromise() throws an error */
  const {
    results: [
      firstPromiseResult,
      secondPromiseResult, // it contains thirdPromiseResult
      thirdPromiseResult, // it is undefined
    ],
    errors, // it contains array of single error of the second promise
  } = await settleAll([
    firstPromise(),
    secondPromise(),
    thirdPromise(),
  ]);

To my mind, it would be much more intuitive if the order of resolved promises and errors preserved:

  /* As an example, secondPromise() throws an error */
  const {
    results: [
      firstPromiseResult,
      secondPromiseResult, // it contains null/undefined
      thirdPromiseResult, // it contains thirdPromiseResult
    ],
    errors, // it contains [null, secondPromiseError, null]
  } = await settleAll([
    firstPromise(),
    secondPromise(),
    thirdPromise(),
  ]);
evenfrost commented 4 years ago

Actually I've just checked the documentation and it says that the order should be preserved:

For resolved promises, the result array contains the resolved value at the same index as the promise. For rejected promises, the result array contains the return value of errFn at the same index as the promise.

So I assume this is a bug. I'm using v1.27.0.