Created
March 27, 2019 01:55
-
-
Save lambdaydoty/86e55ea3187008afd2aaadfefda49c61 to your computer and use it in GitHub Desktop.
#async/await #promise #throw #try/catch
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
async function main () { | |
async function asyncThrows (id) { | |
return new Promise((res, rej) => { | |
console.log(`ASYNC ERROR ${id}`) | |
throw `ASYNC ERROR ${id}` | |
}) | |
} | |
/* sequentially */ | |
// try { | |
// // let a = await asyncThrows(1) | |
// // let b = await asyncThrows(2) | |
// } catch (e) { | |
// console.log(e, 'CAUGHT') | |
// return 'OK' | |
// } | |
/* parallelly */ | |
let a = asyncThrows(1) | |
let b = asyncThrows(2) | |
try { | |
await a | |
} catch (e) { | |
console.log(e, 'CAUGHT') | |
} | |
try { | |
await b | |
} catch (e) { | |
console.log(e, 'CAUGHT') | |
} | |
return '*OK*' | |
} | |
main() | |
.then(console.log) | |
.catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment