go-go-try 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +4 -3
- package/index.js +6 -2
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
declare type ResultTuple<T> = [
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
declare type ResultTuple<T> = readonly [undefined, T] | readonly [string, undefined];
|
|
2
|
+
declare function goTry<T>(value: PromiseLike<T>): PromiseLike<ResultTuple<T>>;
|
|
3
|
+
declare function goTry<T>(value: () => T): ResultTuple<T>;
|
|
4
|
+
export default goTry;
|
package/index.js
CHANGED
|
@@ -21,11 +21,14 @@ function toErrorWithMessage(maybeError) {
|
|
|
21
21
|
function getErrorMessage(error) {
|
|
22
22
|
return toErrorWithMessage(error).message;
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
function isPromise(p) {
|
|
25
|
+
return pIsPromise(p);
|
|
26
|
+
}
|
|
27
|
+
function goTry(value) {
|
|
25
28
|
let unwrappedValue;
|
|
26
29
|
try {
|
|
27
30
|
unwrappedValue = typeof value === 'function' ? value() : value;
|
|
28
|
-
if (
|
|
31
|
+
if (isPromise(unwrappedValue)) {
|
|
29
32
|
return Promise.resolve(unwrappedValue)
|
|
30
33
|
.then((value) => [undefined, value])
|
|
31
34
|
.catch((err) => [getErrorMessage(err), undefined]);
|
|
@@ -36,3 +39,4 @@ export default function goTry(value) {
|
|
|
36
39
|
return [getErrorMessage(err), undefined];
|
|
37
40
|
}
|
|
38
41
|
}
|
|
42
|
+
export default goTry;
|