-
-
Save AlexWebLab/9ab4a24c06ec488e7d26fff0d3c6884c to your computer and use it in GitHub Desktop.
Revisions
-
t3dotgg revised this gist
Feb 24, 2025 . 1 changed file with 0 additions and 8 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,12 +21,4 @@ export async function tryCatch<T, E = Error>( } catch (error) { return { data: null, error: error as E }; } } -
t3dotgg created this gist
Feb 24, 2025 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ // Types for the result object with discriminated union type Success<T> = { data: T; error: null; }; type Failure<E> = { data: null; error: E; }; type Result<T, E = Error> = Success<T> | Failure<E>; // Main wrapper function export async function tryCatch<T, E = Error>( promise: Promise<T>, ): Promise<Result<T, E>> { try { const data = await promise; return { data, error: null }; } catch (error) { return { data: null, error: error as E }; } } async function somePromise() { const response = await fetch("https://api.example.com/data"); if (!response.ok) { throw new Error("Network response was not ok"); } return (await response.json()) as { username: string }; }