Skip to content

Commit

Permalink
refactor: rename catch parameter to mapErr in trySync and tryAsync fu…
Browse files Browse the repository at this point in the history
…nctions for improved clarity
  • Loading branch information
braden-w committed Dec 24, 2024
1 parent 206c85b commit 51ab468
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-bags-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@epicenterhq/result": minor
---

Rename catch parameter to mapErr in trySync and tryAsync functions for improved clarity
12 changes: 6 additions & 6 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ export const Err = <E>(error: E): Err<E> => ({ ok: false, error });

export function trySync<T, E>({
try: operation,
catch: mapError,
mapErr,
}: {
try: () => T extends Promise<unknown> ? never : T;
catch: (error: unknown) => E;
mapErr: (error: unknown) => E;
}): Result<T, E> {
try {
const data = operation();
return Ok(data);
} catch (error) {
return Err(mapError(error));
return Err(mapErr(error));
}
}

export async function tryAsync<T, E>({
try: operation,
catch: mapError,
mapErr,
}: {
try: () => Promise<T>;
catch: (error: unknown) => E;
mapErr: (error: unknown) => E;
}): Promise<Result<T, E>> {
try {
const data = await operation();
return Ok(data);
} catch (error) {
return Err(mapError(error));
return Err(mapErr(error));
}
}

0 comments on commit 51ab468

Please sign in to comment.