Let s imagine we have a file containing next JS code:
process.nextTick(()=>{
console.log( nextTick )
})
queueMicrotask(()=>{
console.log( queueMicrotask )
})
console.log( console.log )
and we ve set module system to "type": "commonjs"
in our package.json
What output in console do we expect? Node.js official documentation says:
So, in console we expect
console.log
nextTick
queueMicrotask
And that works. Until I change module system to "type": "module"
.
After that change I constantly have queueMicrotask executed before process.nextTick .
Console output is:
console.log
queueMicrotask
nextTick
Can anybody explain this behaviour? I assume that this behaviour is somehow related to module evaluation and execution process and that nextTick queueMicrotask somehow get into different event loop ticks as module implies async execution(in browser). Still, that guess is very shaky and from my point of view, is illogical. Nevertheless, I still don t have a plausible explanation.