Created
January 28, 2018 20:27
-
-
Save alexsasharegan/04a038f66cf38090eeb68cca12af55e4 to your computer and use it in GitHub Desktop.
Type safe error handling for javascript promises.
This file contains 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
type Result<T> = | |
| { | |
ok: true | |
value: T | |
} | |
| { | |
ok: false | |
error: any | |
} | |
export async function WrapErr<T>(p: Promise<T>): Promise<Result<T>> { | |
try { | |
return { | |
ok: true, | |
value: await p, | |
} | |
} catch (error) { | |
return { | |
ok: false, | |
error, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment