Make a promise always fulfill with its actual fulfillment value or rejection reason
Installations
npm install p-reflect
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=12
Node Version
14.19.3
NPM Version
8.3.2
Score
99.4
Supply Chain
99.4
Quality
75.4
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (65.14%)
TypeScript (34.86%)
Developer
sindresorhus
Download Statistics
Total Downloads
103,793,500
Last Day
10,293
Last Week
487,154
Last Month
2,106,191
Last Year
28,383,293
GitHub Statistics
53 Stars
13 Commits
8 Forks
7 Watching
1 Branches
5 Contributors
Sponsor this package
Package Meta Information
Latest Version
3.1.0
Package Id
p-reflect@3.1.0
Unpacked Size
6.95 kB
Size
2.57 kB
File Count
5
NPM Version
8.3.2
Node Version
14.19.3
Total Downloads
Cumulative downloads
Total Downloads
103,793,500
Last day
-43.2%
10,293
Compared to previous day
Last week
-7.8%
487,154
Compared to previous week
Last month
-9.8%
2,106,191
Compared to previous month
Last year
-14.7%
28,383,293
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
p-reflect
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 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.
1import pReflect from 'p-reflect'; 2 3const promises = [ 4 getPromise(), 5 getPromiseThatRejects(), 6 getPromise() 7]; 8 9const results = await Promise.all(promises.map(pReflect)); 10 11console.log(results); 12/* 13[ 14 { 15 status: 'fulfilled', 16 value: '🦄' 17 isFulfilled: true, 18 isRejected: false 19 }, 20 { 21 status: 'rejected', 22 reason: [Error: 👹] 23 isFulfilled: false, 24 isRejected: true 25 }, 26 { 27 status: 'fulfilled', 28 value: '🐴' 29 isFulfilled: true, 30 isRejected: false 31 } 32] 33*/ 34 35const resolvedString = results 36 .filter(result => result.isFulfilled) 37 .map(result => result.value) 38 .join(''); 39 40console.log(resolvedString); 41//=> '🦄🐴'
The above is just an example. Use p-settle
if you need exactly that.
API
pReflect(promise)
Returns a Promise<Object>
.
The object has the following properties:
status
('fulfilled'
or'rejected'
, depending on how the promise resolved)value
orreason
(Depending on whether the promise fulfilled or rejected)isFulfilled
isRejected
promise
Type: Promise
A promise to reflect upon.
isFulfilled(object)
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
- reference documentation Using type predicates
isRejected(object)
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
.
Related
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 5/13 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/p-reflect/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/sindresorhus/p-reflect/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:21
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 5 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More