Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Created March 17, 2022 09:15
Show Gist options
  • Save wmakeev/6d2995b609ceb947ed965be202eb5e25 to your computer and use it in GitHub Desktop.
Save wmakeev/6d2995b609ceb947ed965be202eb5e25 to your computer and use it in GitHub Desktop.
[Error helpers] #error #resolve
export const resolve = async <T>(promise: Promise<T>) => {
try {
const result = await promise
return [result, null] as const
} catch (err) {
if (err instanceof Error) {
return [null, err] as const
} else {
throw err
}
}
}
export const tryCall = <T>(thunk: () => T) => {
try {
const result = thunk()
return [result, null] as const
} catch (err) {
if (err instanceof Error) {
return [null, err] as const
} else {
throw err
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment