Skip to content

Instantly share code, notes, and snippets.

@hoangbits
Last active September 13, 2021 09:24
Show Gist options
  • Save hoangbits/f5eafbbba687a30ea63bbd062532decc to your computer and use it in GitHub Desktop.
Save hoangbits/f5eafbbba687a30ea63bbd062532decc to your computer and use it in GitHub Desktop.
handle UnhandledPromiseRejection
/**
Ref: https://jonasjancarik.medium.com/handling-those-unhandled-promise-rejections-when-using-javascript-async-await-and-ifee-5bac52a0b29f
Placement of catch BEFORE and AFTER then: https://stackoverflow.com/questions/42013104/placement-of-catch-before-and-after-then
*/
// 1. Work
(async()=>{
return Promise.reject("a").catch((_error) =>{})
// Promise { <state>: "fulfilled", <value>: undefined }
})()
(async()=>{
try{
return await Promise.reject("a")
}catch(_error){}
// Promise { <state>: "fulfilled", <value>: undefined }
})()
// 2.NOT work variation
(async()=>{
return Promise.reject("a").catch()
// Promise { <state>: "rejected", <reason>: "a" }
// Uncaught (in promise) a
})()
(async()=>{
try{
return Promise.reject("a")
}catch(_error){}
// Promise { <state>: "rejected", <reason>: "a" }
// Uncaught (in promise) a
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment