Skip to content

Instantly share code, notes, and snippets.

@lambdaydoty
Created March 27, 2019 01:55
Show Gist options
  • Save lambdaydoty/86e55ea3187008afd2aaadfefda49c61 to your computer and use it in GitHub Desktop.
Save lambdaydoty/86e55ea3187008afd2aaadfefda49c61 to your computer and use it in GitHub Desktop.
#async/await #promise #throw #try/catch
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