Created
March 17, 2022 09:15
-
-
Save wmakeev/6d2995b609ceb947ed965be202eb5e25 to your computer and use it in GitHub Desktop.
[Error helpers] #error #resolve
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 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 | |
} | |
} | |
} |
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 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