Created
July 24, 2023 11:44
-
-
Save metruzanca/0f678e0e9de37545cd8d0775cc63b5fa to your computer and use it in GitHub Desktop.
Rust & Golang inspired error handing in Node
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 Success<Data> = readonly [Data, undefined]; | |
export type Failure<Err = Error> = readonly [undefined, Err]; | |
/** A cross between Rust and Golang exception handling. I greatly dislike exceptions */ | |
export type Result<Data, Err = Error> = Promise<(Success<Data> | Failure<Err>)> | |
export const Ok = <T>(data: T): Success<T> => [data, undefined] | |
export const Err = <E = Error>(error: E): Failure<E> => [undefined, error] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e.g.