You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Narrows a variable of type `PromiseResult` to type `PromiseFulfilledResult` if the variable has the property `value`, otherwise narrows it to the `PromiseRejectedResult` type.
78
+
*/
79
+
exportfunctionisFulfilled<T>(promiseResult: PromiseResult<T>): promiseResult is PromiseFulfilledResult<T>;
80
+
81
+
/**
82
+
Narrows a variable of type `PromiseResult` to type `PromiseRejectedResult` if the variable has the property `reason`, otherwise narrows it to the `PromiseFulfilledResult` type.
83
+
*/
84
+
exportfunctionisRejected<T>(promiseResult: PromiseResult<T>): promiseResult is PromiseRejectedResult;
Copy file name to clipboardExpand all lines: readme.md
+30-7
Original file line number
Diff line number
Diff line change
@@ -29,19 +29,22 @@ console.log(results);
29
29
/*
30
30
[
31
31
{
32
-
isFulfilled: true,
33
-
isRejected: false,
32
+
status: 'fulfilled',
34
33
value: '🦄'
34
+
isFulfilled: true,
35
+
isRejected: false
35
36
},
36
37
{
37
-
isFulfilled: false,
38
-
isRejected: true,
38
+
status: 'rejected',
39
39
reason: [Error: 👹]
40
+
isFulfilled: false,
41
+
isRejected: true
40
42
},
41
43
{
42
-
isFulfilled: true,
43
-
isRejected: false,
44
+
status: 'fulfilled',
44
45
value: '🐴'
46
+
isFulfilled: true,
47
+
isRejected: false
45
48
}
46
49
]
47
50
*/
@@ -65,16 +68,36 @@ Returns a `Promise<Object>`.
65
68
66
69
The object has the following properties:
67
70
71
+
-`status`*(`'fulfilled'` or `'rejected'`, depending on how the promise resolved)*
72
+
-`value` or `reason`*(Depending on whether the promise fulfilled or rejected)*
68
73
-`isFulfilled`
69
74
-`isRejected`
70
-
-`value` or `reason`*(Depending on whether the promise fulfilled or rejected)*
71
75
72
76
#### promise
73
77
74
78
Type: `Promise`
75
79
76
80
A promise to reflect upon.
77
81
82
+
### isFulfilled(object)
83
+
84
+
This is a type guard for TypeScript users.
85
+
86
+
Returns `true` if the object has the property `value`, `false` otherwise.
87
+
88
+
This is useful since `await pReflect(promise)` always returns a `PromiseResult`. This function can be used to determine whether `PromiseResult` is `PromiseFulfilledResult` or `PromiseRejectedResult`.
89
+
90
+
This is a workaround for [microsoft/TypeScript#32399](https://github.com/microsoft/TypeScript/issues/32399)
91
+
- reference documentation [Using type predicates](https://www.typescriptlang.org/docs/handbook/2/narrowing.html)
92
+
93
+
### isRejected(object)
94
+
95
+
This is a type guard for TypeScript users.
96
+
97
+
Returns `true` if the object has the property `reason`, `false` otherwise.
98
+
99
+
This is useful since `await pReflect(promise)` always returns a `PromiseResult`. This function can be used to determine whether `PromiseResult` is `PromiseRejectedResult` or `PromiseFulfilledResult`.
100
+
78
101
## Related
79
102
80
103
-[p-settle](https://github.com/sindresorhus/p-settle) - Settle promises concurrently and get their fulfillment value or rejection reason
0 commit comments