Last active
October 7, 2022 16:03
-
-
Save rimzzlabs/9dc4db82333f92c8491ca8f6a2f099f8 to your computer and use it in GitHub Desktop.
Avoid async / await try - catch hell
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
export async function httpreq(fn: () => Promise<any>): Promise<[any, any]> { | |
try { | |
const res = await fn() | |
return [res, null] | |
} catch(err) { | |
return [null, err] | |
} | |
} | |
// usage | |
const request = async () => await fetch('https://foo.bizz') | |
const [data, error] = await httpreq(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment