Skip to content

Instantly share code, notes, and snippets.

@mucaho
Last active February 22, 2021 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mucaho/74cfd78fa5b515eae910eeff42dfe819 to your computer and use it in GitHub Desktop.
Save mucaho/74cfd78fa5b515eae910eeff42dfe819 to your computer and use it in GitHub Desktop.
Example code demonstrating the not immediately apparent issue of a promise rejecting while another one is being awaited.
async function main() {
const rej = Promise.reject('err')
// the following line is needed,
// otherwise UnhandledPromiseRejectionWarning is thrown
// which can't be caught anywhere else (not even by async caller)!
// can be NOOP though, if error handling may be delayed to below catch block
rej.catch(e => console.log(e)) // logs 'err'
const del = new Promise((resolve) => {
setTimeout(() => resolve('del'), 100)
})
try {
await del
} catch (e) {
console.log(e)
}
try {
await rej
} catch (e) {
console.log(e) // logs 'err'
}
console.log('fin') // logs 'fin'
}
/**
* OUTPUT:
* err
* err
* fin
*/
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment