Executes an array of promises and returns their resolved or rejected results.
npm install --save @trenskow/results
const results = require('@trenskow/results');
const [resolved, rejected] = results([
Promise.resolve('a'),
Promise.reject(new Error('a')),
Promise.resolve('b'),
Promise.reject(new Error('b'))
]);
/* -> [['a','b'],[Error('a'), Error('b')]] */
const resolved = results.resolved([
Promise.resolve('a'),
Promise.reject(new Error('a')),
Promise.resolve('b'),
Promise.reject(new Error('b'))
]);
/* -> ['a','b'] */
const rejected = results.rejected([
Promise.resolve('a'),
Promise.reject(new Error('a')),
Promise.resolve('b'),
Promise.reject(new Error('b'))
]);
/* -> [Error('a'),Error('b')] */
The promises are executed in serial and therefore out-of-order, meaning there is no guarantee in which order the result comes out.
You can specify how many promises can be executed at once by using an option.
const [resolved, rejected] = results(/* promises */, { simultaneously: 1 });
The above example will make the promises execute one at the time (which also guarantees order). You can specify any arbitrary number, default is 0
, which is equal to unlimited.
This works for all three variants (resolved/rejected, resolved and rejected).
MIT (see license).