3

The following code:

process.nextTick( () => console.log("nextTick 1") );
process.nextTick( () => { 
    console.log("nextTick 2"); 
    Promise.resolve("Resolved promise inside nextTick").then(console.log); 
})

Promise.resolve("Resolved promise 1").then(console.log);
Promise.reject("Rejected promise 2").catch(console.log);

setImmediate( () => {
    console.log("setImmediate");
    Promise.resolve("Resolved promise inside setImmediate").then(console.log);
    Promise.reject("Rejected promise inside setImmediate").catch(console.log);
    process.nextTick( () => console.log("nextTick 3") );
})

outputs this in node v14.17.0

Resolved promise 1
Rejected promise 2
nextTick 1
nextTick 2
Resolved promise inside nextTick    
setImmediate
nextTick 3
Resolved promise inside setImmediate
Rejected promise inside setImmediate

Why are the first two Promise.then/catch callbacks called before the process.nextTick callbacks ? Shouldn´t process.nextTick callbacks preceed Promise callbacks like in the setImmediate part ?

trk
  • 53
  • 5
Byte
  • 71
  • 3
  • 1
    promises .then are processed in the microtask queue, which is "higher priority" than the event queue where nextTick is processed – Jaromanda X Jun 12 '21 at 14:10
  • 1
    @JaromandaX But that would mean `nextTick 3` should come after the promise handlers that are scheduled from the `setImmediate` callback – Bergi Jun 12 '21 at 14:10
  • @Bergi thats the thing what confuses me – Byte Jun 12 '21 at 14:11
  • 1
    @Bergi - I don't know what setImmediate does :p - oh, yeah .. just reading about it, that really does make no sense!!! – Jaromanda X Jun 12 '21 at 14:11
  • Related (but not really an answer): [How are node Promises getting in between `nextTick` and `setImmediate`?](https://stackoverflow.com/q/32453877/1048572) and [setImmediate vs. nextTick](https://stackoverflow.com/q/15349733/1048572). I suspect this is somehow explained in https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/ - or it's a bug. – Bergi Jun 12 '21 at 14:15
  • 1
    here is stated that process.nextTick > promise callbacks -> https://github.com/nodejs/help/issues/1789 – Byte Jun 12 '21 at 14:20
  • How are you executing this script? On my machine (macOS) nextTicks fire before in any case when running this script. (node v10, v14.14.0, v15.5.1) – Kaiido Jun 14 '21 at 03:00
  • Im on a windows 10 machine. Execute it in VS Code with: node script.js – Byte Jun 14 '21 at 04:03
  • very weird, [even in tio](https://tio.run/##lZLBCsIwDIbvfYrQU3uwMD0OvXsT8QVGG1zn1o61DEH27LNzTDrZUHtJIfl@8kGKrM2cbHTtN8Yq7Pu6sRKdEwbv/qLljQHjsD@AtMbZEkVpr4xOTUgoB56SNeixgm0pT@HU2Eo7FA2GiRYZPY8fBfXYAW2cVggTRrnwORoWZYaYjhPyPSpZhCOwQOkHbqgRFzYVMvMy/wCJQ3@sKlQ68/jWJRDeTDkeC9KvgZ/F5/DS/vO4FYfltCWrIe6/A9iNB9Dxvn8C) they get the nextTicks first (they use node v11.6.0) I don't have a Windows machine at hand, could you try to run your script from a Terminal or equivalent instance directly? – Kaiido Jun 14 '21 at 04:42
  • TIO uses Node Version 11.6.0. If I run my script on my machine with the same version it works as expected. Maybe it has something to do with the node version. I also tried version 13.14.0 but this time it prints the same output as in my question – Byte Jun 15 '21 at 03:42
  • Very very weird, as I said I tried the version you mentionned on my machine and couldn't reproduce... Now wondering if this could be flaky somehow... – Kaiido Jun 15 '21 at 14:44
  • Yeah very weird. Seems that here is nobody who can answer this question. Even the node people.. – Byte Jun 15 '21 at 18:10
  • @Byte You might try opening a regression bug report with nodejs – Bergi Jun 15 '21 at 18:16

1 Answers1

0

Simplest way to reproduce this behavior (node 14+, 16+, 18):

Add a new package.json to the same folder with just these lines:

{
  "type": "commonjs" //I assume you have here "module"
} 

With commonJS the output is the expected one.

Unfortunately there is not a good explanation for this yet, more discussion here