Created
August 14, 2021 04:08
-
-
Save nguyenit67/26ed45d0ee843a8ac8acbbbd681b7c74 to your computer and use it in GitHub Desktop.
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
// #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