when I run
process.nextTick(() => console.log(8))
Promise.resolve("hi").then(t => console.log(t))
console.log(7);
Output is 7 8 hi
The output is expected because process.nextTick runs after the stack is empty and before we start processing the task queue. But when I run
function main() {
console.log(1);
console.log(2);
func();
console.log(6);
}
function func() {
console.log(4);
console.log(5);
Promise.resolve() // callback gets pushed to microtask queue
.then(() => { // this callback is in callstack now after logging 6
process.nextTick(() => console.log(8))
Promise.resolve("hi").then(t => console.log(t))
console.log(7);
})
}
Output is 1 2 4 5 6 7 hi 8
Why does the last part log 7 hi 8 instead of 7 8 hi?