readme.md 1.7 KB

p-reflect Build Status

Make a promise always fulfill with its actual fulfillment value or rejection reason

Useful when you want a promise to fulfill no matter what and would rather handle the actual state afterwards.

Install

$ npm install --save p-reflect

Usage

Here, Promise.all would normally fail early because one of the promises rejects, but by using p-reflect, we can ignore the rejection and handle it later on.

const pReflect = require('p-reflect');

const promises = [
    getPromise(),
    getPromiseThatRejects(),
    getPromise()
];

Promise.all(promises.map(reflect)).then(result => {
    console.log(result);
    /*
    [{
        isFulfilled: true,
        isRejected: false,
        value: '🦄'
    },
    {
        isFulfilled: false,
        isRejected: true,
        reason: [Error: 👹]
    },
    {
        isFulfilled: true,
        isRejected: false,
        value: '🐴'
    }]
    */

    const ret = f.filter(x => x.isFulfilled).map(x => x.value).join('');
    console.log(ret);
    //=> '🦄🐴'
});

The above is just an example. Use <code>p-settle</code> if you need this.

API

pReflect(input)

Returns a fulfilled Promise.

The fulfilled value is an object with the following properties:

  • isFulfilled
  • isRejected
  • value or reason (Depending on whether the promise fulfilled or rejected)

input

Type: Promise

  • p-settle - Settle promises concurrently and get their fulfillment value or rejection reason
  • More…

License

MIT © Sindre Sorhus