Skip to content

Commit 0e6850f

Browse files
feat(Promise): added defaultResult parameter to tryCatch
1 parent 25f1f20 commit 0e6850f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/promise/proto/try-catch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ declare global {
2424
*
2525
* ```
2626
* * * *
27+
* @param defaultResult Setting this will put the value into the result field when the promise throws error
2728
*/
28-
tryCatch<E = any>(): Promise<[E, T]>;
29+
tryCatch<E = any>(defaultResult?: T): Promise<[E, T]>;
2930
}
3031
}
3132

32-
Promise.prototype.tryCatch = function<T, E>(this: Promise<T>): Promise<[E, T]> {
33-
return tryCatch<T, E>(this);
33+
Promise.prototype.tryCatch = function<T, E>(this: Promise<T>, defaultResult: T = null): Promise<[E, T]> {
34+
return tryCatch<T, E>(this, defaultResult);
3435
};

src/promise/try-catch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
* ```
3939
* * * *
4040
* @param promise Promise to try
41+
* @param defaultResult Setting this will put the value into the result field when the promise throws error
4142
* @returns Error and result array
4243
*/
43-
export async function tryCatch<T, E = any>(promise: Promise<T>): Promise<[E, T]> {
44+
export async function tryCatch<T, E = any>(promise: Promise<T>, defaultResult: T = null): Promise<[E, T]> {
4445
try {
4546
const result = await promise;
4647
return [null, result];
4748
} catch (error) {
48-
return [error, null];
49+
return [error, defaultResult];
4950
}
5051
}

0 commit comments

Comments
 (0)