Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
p-reflect
Advanced tools
Make a promise always fulfill with its actual fulfillment value or rejection reason
Weekly downloads
Readme
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 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.
import pReflect from 'p-reflect';
const promises = [
getPromise(),
getPromiseThatRejects(),
getPromise()
];
const results = await Promise.all(promises.map(pReflect));
console.log(results);
/*
[
{
status: 'fulfilled',
value: '🦄'
isFulfilled: true,
isRejected: false
},
{
status: 'rejected',
reason: [Error: 👹]
isFulfilled: false,
isRejected: true
},
{
status: 'fulfilled',
value: '🐴'
isFulfilled: true,
isRejected: false
}
]
*/
const resolvedString = results
.filter(result => result.isFulfilled)
.map(result => result.value)
.join('');
console.log(resolvedString);
//=> '🦄🐴'
The above is just an example. Use p-settle
if you need exactly that.
Returns a Promise<Object>
.
The object has the following properties:
status
('fulfilled'
or 'rejected'
, depending on how the promise resolved)value
or reason
(Depending on whether the promise fulfilled or rejected)isFulfilled
isRejected
Type: Promise
A promise to reflect upon.
This is a type guard for TypeScript users.
Returns true
if the object has the property value
, false
otherwise.
This is useful since await pReflect(promise)
always returns a PromiseResult
. This function can be used to determine whether PromiseResult
is PromiseFulfilledResult
or PromiseRejectedResult
.
This is a workaround for microsoft/TypeScript#32399
This is a type guard for TypeScript users.
Returns true
if the object has the property reason
, false
otherwise.
This is useful since await pReflect(promise)
always returns a PromiseResult
. This function can be used to determine whether PromiseResult
is PromiseRejectedResult
or PromiseFulfilledResult
.
FAQs
Make a promise always fulfill with its actual fulfillment value or rejection reason
The npm package p-reflect receives a total of 425,467 weekly downloads. As such, p-reflect popularity was classified as popular.
We found that p-reflect demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.