Skip to content

Instantly share code, notes, and snippets.

@papalardo
Created January 14, 2025 16:47
Show Gist options
  • Save papalardo/f222f4c8a24f73880fe873d17343edf5 to your computer and use it in GitHub Desktop.
Save papalardo/f222f4c8a24f73880fe873d17343edf5 to your computer and use it in GitHub Desktop.
Trabalhar sem usos de try/catch e melhor legibilidade do fluxo
export type Either<A, B> = { result: A, error: undefined? } | { result: undefined, error: B };
export const either = async <R, L>(getter: () => Promise<R>): Promise<Either<R, L>> => {
try {
return { result: await getter() };
} catch (error) {
return { error };
}
};
const { error, result } = await either(async () => {
throw new Error("Fatal");
})
console.log(result);
console.error(error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment