-
-
Save themojilla/dbc95cd4821d603861516323b27d5a7d to your computer and use it in GitHub Desktop.
Try-catch helper for promises and async/await
This file contains 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 default async function tryCatch<T>( | |
promise: Promise<T>, | |
): Promise<{ error: Error } | { data: T }> { | |
try { | |
return { data: await promise }; | |
} catch (error) { | |
return { error }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment