p-reflect
Advanced tools
Comparing version 3.0.0 to 3.1.0
export interface PromiseFulfilledResult<ValueType> { | ||
status: 'fulfilled'; | ||
value: ValueType; | ||
isFulfilled: true; | ||
isRejected: false; | ||
value: ValueType; | ||
} | ||
export interface PromiseRejectedResult { | ||
status: 'rejected'; | ||
reason: unknown; | ||
isFulfilled: false; | ||
isRejected: true; | ||
reason: unknown; | ||
} | ||
@@ -41,15 +43,18 @@ | ||
{ | ||
status: 'fulfilled', | ||
value: '🦄' | ||
isFulfilled: true, | ||
isRejected: false, | ||
value: '🦄' | ||
isRejected: false | ||
}, | ||
{ | ||
status: 'rejected', | ||
reason: [Error: 👹] | ||
isFulfilled: false, | ||
isRejected: true, | ||
reason: [Error: 👹] | ||
isRejected: true | ||
}, | ||
{ | ||
status: 'fulfilled', | ||
value: '🐴' | ||
isFulfilled: true, | ||
isRejected: false, | ||
value: '🐴' | ||
isRejected: false | ||
} | ||
@@ -71,1 +76,11 @@ ] | ||
>; | ||
/** | ||
Narrows a variable of type `PromiseResult` to type `PromiseFulfilledResult` if the variable has the property `value`, otherwise narrows it to the `PromiseRejectedResult` type. | ||
*/ | ||
export function isFulfilled<T>(promiseResult: PromiseResult<T>): promiseResult is PromiseFulfilledResult<T>; | ||
/** | ||
Narrows a variable of type `PromiseResult` to type `PromiseRejectedResult` if the variable has the property `reason`, otherwise narrows it to the `PromiseFulfilledResult` type. | ||
*/ | ||
export function isRejected<T>(promiseResult: PromiseResult<T>): promiseResult is PromiseRejectedResult; |
18
index.js
@@ -6,13 +6,23 @@ export default async function pReflect(promise) { | ||
return { | ||
status: 'fulfilled', | ||
value, | ||
isFulfilled: true, | ||
isRejected: false, | ||
value | ||
isRejected: false | ||
}; | ||
} catch (error) { | ||
return { | ||
status: 'rejected', | ||
reason: error, | ||
isFulfilled: false, | ||
isRejected: true, | ||
reason: error | ||
isRejected: true | ||
}; | ||
} | ||
} | ||
export function isFulfilled(promiseResult) { | ||
return 'value' in promiseResult; | ||
} | ||
export function isRejected(promiseResult) { | ||
return 'reason' in promiseResult; | ||
} |
{ | ||
"name": "p-reflect", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Make a promise always fulfill with its actual fulfillment value or rejection reason", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -32,15 +32,18 @@ # p-reflect | ||
{ | ||
status: 'fulfilled', | ||
value: '🦄' | ||
isFulfilled: true, | ||
isRejected: false, | ||
value: '🦄' | ||
isRejected: false | ||
}, | ||
{ | ||
status: 'rejected', | ||
reason: [Error: 👹] | ||
isFulfilled: false, | ||
isRejected: true, | ||
reason: [Error: 👹] | ||
isRejected: true | ||
}, | ||
{ | ||
status: 'fulfilled', | ||
value: '🐴' | ||
isFulfilled: true, | ||
isRejected: false, | ||
value: '🐴' | ||
isRejected: false | ||
} | ||
@@ -69,5 +72,6 @@ ] | ||
- `status` *(`'fulfilled'` or `'rejected'`, depending on how the promise resolved)* | ||
- `value` or `reason` *(Depending on whether the promise fulfilled or rejected)* | ||
- `isFulfilled` | ||
- `isRejected` | ||
- `value` or `reason` *(Depending on whether the promise fulfilled or rejected)* | ||
@@ -80,2 +84,21 @@ #### promise | ||
### 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](https://github.com/microsoft/TypeScript/issues/32399) | ||
- reference documentation [Using type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html) | ||
### 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 | ||
@@ -82,0 +105,0 @@ |
7113
95
105