Last active
August 15, 2024 16:01
-
-
Save jln13x/dd0361b45e76a57006d94b41d27513e2 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
| export const safeTry = async <T>( | |
| promise: Promise<T> | |
| ): Promise<[T, null] | [null, Error]> => { | |
| try { | |
| const data = await promise; | |
| return [data, null]; | |
| } catch (throwable) { | |
| if (throwable instanceof Error) return [null, throwable]; | |
| throw throwable; | |
| } | |
| }; | |
| // Example usage | |
| const [data, error] = await safeTry(getData()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment