Skip to content

Instantly share code, notes, and snippets.

@themojilla
Forked from karlhorky/try-catch.ts
Created May 6, 2021 00:35
Show Gist options
  • Save themojilla/dbc95cd4821d603861516323b27d5a7d to your computer and use it in GitHub Desktop.
Save themojilla/dbc95cd4821d603861516323b27d5a7d to your computer and use it in GitHub Desktop.
Try-catch helper for promises and async/await
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