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.
$ npm install --save p-reflect
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(pReflect)).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 p-settle
if you need this.
Returns a fulfilled Promise
.
The fulfilled value is an object with the following properties:
isFulfilled
isRejected
value
orreason
(Depending on whether the promise fulfilled or rejected)
Type: Promise
MIT Β© Sindre Sorhus