Skip to content

Instantly share code, notes, and snippets.

@rimzzlabs
Last active October 7, 2022 16:03
Show Gist options
  • Save rimzzlabs/9dc4db82333f92c8491ca8f6a2f099f8 to your computer and use it in GitHub Desktop.
Save rimzzlabs/9dc4db82333f92c8491ca8f6a2f099f8 to your computer and use it in GitHub Desktop.
Avoid async / await try - catch hell
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