I have the below code snippet in NodeJS.
console.log("START");
setImmediate(() => {
console.log("setImmediate 1");
});
setTimeout(() => {
console.log("setTimeout 1");
}, 0);
Promise.resolve("hello").then((obj) => console.log("Promise 1"));
console.log("END");
Most of the time the output I get is as given below:
START
END
Promise 1
setTimeout 1
setImmediate 1
However in some cases I do get the below output:
START
END
Promise 1
setImmediate 1
setTimeout 1
Basically the execution order of the setTimeout
and setImmediate
are changing.
Can someone please make me understand the logic behind this?