Last active
November 5, 2024 03:18
-
-
Save TheLucifurry/947ba83067a14952c3ba4e31cf0cf0e2 to your computer and use it in GitHub Desktop.
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
| // Golang-like error handling util | |
| const [ | |
| result = 'default', // always defined | |
| error // will contain possible error | |
| ] = cope(() => JSON.parse('{"a": 1}')) | |
| // ^^^ Just GitHub Gist preview | |
| /** | |
| * @example ```ts // Basic usage | |
| * const [result, error] = cope(() => JSON.parse('{"a": 1}')) | |
| * if (error) { | |
| * // process error | |
| * return | |
| * } | |
| * // process result | |
| * ``` | |
| * | |
| * @example ```ts // With default value | |
| * const [result = 'default value', syncParseErr] = cope(...) | |
| * result // is always defined | |
| * ``` | |
| * | |
| * @example ```ts // ignore insignificant error | |
| * // prepare executions | |
| * cope(()=>{ window.scrollTo(someElement.offsetHeight) }) | |
| * // guaranteed executions | |
| * ``` | |
| */ | |
| export type NullaryFn = () => any | |
| export type NullaryAsyncFn = () => Promise<any> | |
| export type CopeOk<T> = [T, null] | |
| export type CopeErr<E extends Error> = [null, E] | |
| export type CopeResult<T, E extends Error> = CopeOk<T> | CopeErr<E> | |
| export const ok = <T = void>(value: T): CopeOk<T> => [value, null] | |
| export const err = <E extends Error>(error: E): CopeErr<E> => [null, error] | |
| export function cope< | |
| Throws extends Error = Error, | |
| Executor extends NullaryFn | NullaryAsyncFn = NullaryFn | NullaryAsyncFn, | |
| Result extends ReturnType<Executor> = ReturnType<Executor>, | |
| >(executor: Executor): Result extends Promise<any> ? Promise<CopeResult<Awaited<Result>, Throws>> : CopeResult<Result, Throws> { | |
| try { | |
| const result = executor() | |
| // @ts-expect-error Complex type flow | |
| return result instanceof Promise ? result.then(ok, err) : ok(result) | |
| } | |
| catch (error) { | |
| // @ts-expect-error Complex type flow | |
| return err(error as Throws) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment