i'm trying to understand the output of a test that i've been running because it seems to go against everything that i'm reading and hearing about how the node.js event loop works. supposedly all callbacks in the nextTick queue are executed before all callbacks in the promise queue but when i run this snippet
Promise.resolve().then(() => console.log('promise'))
process.nextTick(() => console.log('nextTick'))
i get this
promise
nextTick
i get the same output if i change the code to this
new Promise((resolve, reject) => resolve('promise')).then((resolve) => console.log(resolve))
process.nextTick(() => console.log('nextTick'))
why is that? i'm running v21.7.2 of node btw
this is one of the videos that i've watched that states that nextTick always runs before promises. (the video plays right at the point where an example is given) Node.js Tutorials - 43 - Microtask Queues
then
is always putting the callback in the microtask queue, no matter when the promise is being (or was) resolved. – GiorgionesetTimeout
with a 0ms delay. – Cisnerosthen()
call that pushes the callback into the microtask queue, it's whatever task resolved the promise. This may give funky ordering stuff when doing so from node's nextTick + promise-queue, microtask queues, since they'll loop to each other before returning to the actual event-loop. (But their point that a.then()
callback always ends up in a microtask is very valid). – Uhl