-
-
Save AndreiCalazans/71a9469b6600a43536b7b407063e51c4 to your computer and use it in GitHub Desktop.
Try-catch helper for promises and async/await
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 default async function tryCatch<Data>( | |
promise: Promise<Data>, | |
): Promise<{ error: Error } | { data: Data }> { | |
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