Skip to content

Instantly share code, notes, and snippets.

@nguyenit67
Created August 14, 2021 04:08
Show Gist options
  • Save nguyenit67/26ed45d0ee843a8ac8acbbbd681b7c74 to your computer and use it in GitHub Desktop.
Save nguyenit67/26ed45d0ee843a8ac8acbbbd681b7c74 to your computer and use it in GitHub Desktop.
Async Await try-catch hell
// #1 catch in promise-based
await step1().catch(fun);
async function gettingBetter() {
const a = await step1().catch(err => handle(err));
const b = await step2().catch(err => handle(err));
const c = await step3().catch(err => handle(err));
}
// #2 create a function handle try catch => [data, error]
async function awesome() {
try {
const data = await promise;
return [data, null];
} catch (error) {
console.error(error);
return [null, error];
}
}
async function main() {
const [data, error] = await awesome();
if (error) {
// handle error here
}
const [data2, error2] = await awesome();
const [data3, error3] = await awesome();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment