Created
February 26, 2025 16:28
-
-
Save maddsua/04e1e41a1114e9d5a58baa104fbe290c 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
| type Expect<T> = { | |
| result: T; | |
| error: null; | |
| } | { | |
| result: null; | |
| error: Error; | |
| }; | |
| type Result<T> = Promise<T> | T; | |
| const expect = async <T>(cb: Result<T>): Promise<Expect<T>> => { | |
| try { | |
| return { result: await cb, error: null }; | |
| } catch (error) { | |
| if (error instanceof Error) { | |
| return { result: null, error: error }; | |
| } | |
| return { result: null, error: new Error(`${error}`) }; | |
| } | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: