Last active
September 13, 2021 09:24
-
-
Save hoangbits/f5eafbbba687a30ea63bbd062532decc to your computer and use it in GitHub Desktop.
handle UnhandledPromiseRejection
This file contains hidden or 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
/** | |
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