Created
January 14, 2025 16:47
-
-
Save papalardo/f222f4c8a24f73880fe873d17343edf5 to your computer and use it in GitHub Desktop.
Trabalhar sem usos de try/catch e melhor legibilidade do fluxo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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