0

I am unable to find any documentation on why the order of execution is nextTick queue followed by promise queue for CJS modules whereas promise queue followed by nextTick queue for ES modules. Any insight appreciated!

// index.js (CJS)
Promise.resolve().then(() => console.log("this is Promise.resolve 1"));
process.nextTick(() => console.log("this is process.nextTick 1"));

Logs "this is process.nextTick 1" and then "this is Promise.resolve 1"

// index.mjs (ESM)
Promise.resolve().then(() => console.log("this is Promise.resolve 1"));
process.nextTick(() => console.log("this is process.nextTick 1"));

Logs "this is Promise.resolve 1" and then "this is process.nextTick 1"

Vishwas
  • 1,398
  • 8
  • 20
  • 33
  • Probably because in ESM you're actually in an async function (global async), and thus the microtask end-point would get visited before the nextTick one (since you're basically already in the microtask-checkpoint). Though that might require to be checked in the sources, and I never took the time to do so. (That question has already been asked, but I don't think it ever got an answer). – Kaiido Mar 20 '23 at 06:37
  • 1
    Found it, that was https://stackoverflow.com/questions/70518968 – Kaiido Mar 20 '23 at 06:43
  • Seems like there is no concrete explanation anywhere though righ? – Vishwas Mar 21 '23 at 11:04
  • Does this answer your question? [process.nextTick vs queueMicrotask in commonJs and ESM. What is the execution order?](https://stackoverflow.com/questions/70518968/process-nexttick-vs-queuemicrotask-in-commonjs-and-esm-what-is-the-execution-or) – Kaiido Jun 01 '23 at 03:30

0 Answers0