@trenskow/results

0.1.6 • Public • Published

@trenskow/results

Executes an array of promises and returns their resolved or rejected results.

Installation

npm install --save @trenskow/results

Usage

const results = require('@trenskow/results');

Getting all 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')]] */

Getting Only Resolved Results

const resolved = results.resolved([
	Promise.resolve('a'),
	Promise.reject(new Error('a')),
	Promise.resolve('b'),
	Promise.reject(new Error('b'))
]);
/* -> ['a','b'] */

Getting Only Rejected Results

const rejected = results.rejected([
	Promise.resolve('a'),
	Promise.reject(new Error('a')),
	Promise.resolve('b'),
	Promise.reject(new Error('b'))
]);
/* -> [Error('a'),Error('b')] */

Order

The promises are executed in serial and therefore out-of-order, meaning there is no guarantee in which order the result comes out.

Executing in Parallel

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).

LICENSE

MIT (see license).

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.6
    4
    • latest

Version History

Package Sidebar

Install

npm i @trenskow/results

Weekly Downloads

4

Version

0.1.6

License

MIT

Unpacked Size

6.73 kB

Total Files

7

Last publish

Collaborators

  • trenskow