Skip to content

Commit

Permalink
feat: add types for error response (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathewBoyles authored Apr 20, 2023
1 parent 380ab00 commit e2f8d51
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export type NoTryResult<T> = [Error, T];
export type NoTryResult<T, E = Error> = [E | null, T | null];

export function noTry<T>(fn: () => T, handleErr: (error: Error) => void = error => null): NoTryResult<T> {
const result: NoTryResult<T> = [null, null];
function noop(): void {
return null;
}

export function noTry<T, E = Error>(
fn: () => T,
handleErr: (error: E) => void = noop
): NoTryResult<T, E> {
const result: NoTryResult<T, E> = [null, null];
try {
result[1] = fn();
} catch (err) {
Expand All @@ -11,11 +18,11 @@ export function noTry<T>(fn: () => T, handleErr: (error: Error) => void = error
return result;
}

export async function noTryAsync<T>(
export async function noTryAsync<T, E = Error>(
fn: () => Promise<T>,
handleErr: (error: Error) => void = error => null
): Promise<NoTryResult<T>> {
const result: NoTryResult<T> = [null, null];
handleErr: (error: E) => void = noop
): Promise<NoTryResult<T, E>> {
const result: NoTryResult<T, E> = [null, null];
try {
result[1] = await fn();
} catch (err) {
Expand Down

0 comments on commit e2f8d51

Please sign in to comment.