Skip to content

Commit

Permalink
FEAT: add three alternative functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahme-dev committed May 20, 2023
1 parent 1924bd4 commit 8faf2a0
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/alternative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type Result<T> = T | Error;

export function isError<T>(possibleErr: Result<T>): possibleErr is Error {
return possibleErr instanceof Error;
}

export async function tryAsync<T>(promise: Promise<T>): Promise<Result<T>> {
try {
const ok = await promise;
return ok;
} catch (err) {
return err as Error;
}
}

export function trySync<T>(func: () => T): Result<T> {
try {
const ok = func();
return ok;
} catch (err) {
return err as Error;
}
}

0 comments on commit 8faf2a0

Please sign in to comment.