I have this doubt regarding order of execution of Timer functions , microtasks and event listeners :
let promise = Promise.reject("Err@!");
setTimeout(() => {
promise.catch(er => console.log(er, "Caught !")); // (1)
});
window.addEventListener('unhandledrejection', event => console.log("Unhandled Rejection !", event.reason)); // (2)
// Output
// >> Unhandled Rejection ! Err@!
// >> Err@! Caught !
In this, the rejected promise gets caught by Unhandled Rejection
event before being caught by .catch()
inside setTimeout
, the reason for that is:
An "unhandled rejection" occurs when a promise error is not handled at the end of the microtask queue (https://javascript.info/microtask-queue#unhandled-rejection)
Now Considering another case :
let promise = Promise.reject("Err@!");
setTimeout(() => { // (1) ,gets called first
setTimeout(function () { // (3) , gets called at last , (after (2))
promise.catch(er => console.log("caught ", er ))
})
});
setTimeout(function () { // (2) , gets called after (1)
window.addEventListener('unhandledrejection', event => console.log("Unhandled Rejection !", event.reason));
})
// Output
//>> caught Err@!
In this, the promise gets caught by catch
handler in nested setTimeout
, even when in second setTimeout
, the microtask queue is empty and the rejected promise is still not handled by window event listener...
Why so?