-
-
Save syed-ahmad/dc42c8e447b531f7d66ee5520276d36d 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
// Slightly improved version with destructuring working as expected | |
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