I'm expecting this code to run, then after 2 seconds, the execution stack will become empty and there is one callback of setTimeout. I'm expecting this as I would expect the message queue and the job queue to be empty(as the promise is not resolved yet).
So, the Callback is printed first, then after 3 seconds, the promise is resolved and its callback is placed in the job queue and the message Promise is resolved should be printed. But that is not what is happening. So what I am missing here?
The code is:
setTimeout(function() {
console.log("Callbcack");
}, 0);
new Promise((resolve, reject) => {
console.log("Inside promise");
let ms = 5000 + new Date().getTime();
while (new Date() < ms) {}
resolve("Promise is resolved");
}).then((data) => {console.log(data)})
.catch((err) => {
console.log(err);
})
var ms = 2000 + new Date().getTime();
while (new Date() < ms) {}