Node eventloop problem

setTimeout(() => {
  console.log(1)
  Promise.resolve(3).then(data => console.log(data));
}, 0)

setTimeout(() => {
  console.log(2)
}, 0)

We know that the callback of setTimeout is performed in the timer phase, and
then both setTimeout are registered to the timer phase

when eventloop checks timer, two setTimeout are then executed,
and promise.resolve () in the first setTimeout is registered with microTask,
executes microTask. at the end of the timer phase

but this code executes
with results of 1 2 3 and 1 3 2

so I don"t understand why there are these two different results.

Mar.21,2021

Note: When delay is larger than 2147483647 or less than 1, the delay will be set to 1.

timers_settimeout_callback_delay_args

this depends on whether the timer phase considers the second timer timeout.
then you can change it to

setTimeout(() => {
    console.log(1)
    Promise.resolve(3).then(data => console.log(data));
}, 0)

setTimeout(() => {
    console.log(2)
}, 0)

let d = Date.now();
while (!(Date.now() - d > 1)) {

}
Menu