@ngard/promise-some

1.1.1 • Public • Published

promise-some

source bundle size build status license

A complementary function to Promise.all() that will resolve if any passed Promise resolves and rejects only if all of the passed Promises reject. The rejection handler is passed an array containing the individual Promise's rejection reasons in the order the Promises were passed in to some (not in the order in which they rejected).


Syntax

import { some } from '@ngard/promise-some';

some(/* iterable */)

Parameter

iterable — An iterable object containing Promises or other values

Return

  • An already rejected Promise if the iterable passed is empty.
  • An asynchronously resolved Promise if the iterable passed contains a non-Promise value.
  • A pending Promise in all other cases. This returned promise is then resolved/rejected asynchronously (as soon as the stack is empty) when any of the promises in the given iterable have resolved, or if any of the promises resolves. Returned values will be in order of the Promises passed, regardless of completion order.

Examples

const p1 = Promise.resolve('p1 resolved');
const p2 = new Promise(() => {});

some([ p1, p2 ]).then((val) => console.log(val));
// logs "p1 resolved"
const p1 = Promise.resolve('p1 resolved');

some([ p1, 'not a promise' ]).then((val) => console.log(val));
// logs "not a promise"
const p1 = new Promise((resolve, reject) => {
  setTimeout(reject, 300, 'p1 rejected');
});
const p2 = new Promise((resolve, reject) => {
  setTimeout(reject, 100, 'p2 rejected');
});
const p3 = new Promise((resolve, reject) => {
  setTimeout(reject, 200, 'p3 rejected');
});

some([ p1, p2, p3 ]).catch((val) => console.log(val));
// logs ["p1 rejected", "p2 rejected", "p3 rejected"]

Note: This utility relies on Array.from which is not supported in Internet Explorer. However, Promise is also not supported in IE. If you are polyfilling Promise for older browsers, you will also need to polyfill Array.from.

Package Sidebar

Install

npm i @ngard/promise-some

Weekly Downloads

0

Version

1.1.1

License

MIT

Unpacked Size

15.7 kB

Total Files

7

Last publish

Collaborators

  • ngard