My expectation with this code is that it will run, then after a 2-second delay, the execution stack will become empty with one callback in the setTimeout function. Since the promise is not resolved yet, I anticipate both the message queue and job queue to be empty at this point.
Based on my understanding, the Callback should be printed first, followed by the resolution of the promise after 3 seconds - printing the message "Promise is resolved". However, the actual output differs from my expectations. What am I overlooking here?
Here is the code snippet:
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) {}