Node: Callbacks of setTimeout and setImmediate queued differently in REPL

Created on 4 Apr 2020  路  5Comments  路  Source: nodejs/node

In my understanding the REPL suppose to execute given script the same way as running it from file. What I noticed is that in the REPL mode callbacks of setImmediate are executed after tasks of setTimeout, however it is other way around when running script from file.

  • Version: 13.12.0
  • Platform: Darwin MacBook-Pro-Administrator.local 18.7.0 Darwin Kernel Version 18.7.0: Thu Jan 23 06:52:12 PST 2020; root:xnu-4903.278.25~1/RELEASE_X86_64 x86_64

What steps will reproduce the bug?

  1. Execute following script from file
const test = () => {
  setTimeout(() => console.log('timeout'));
  setImmediate(() => console.log('immediate'));
};

test();
  1. Execute same script pasting it in the REPL mode.

How often does it reproduce? Is there a required condition?

Reproduce-able every time.

What is the expected behavior?

The expected behaviour is that the event loop in the REPL mode runs callbacks of setTimeout and setImmediate in the same order as running from file.

question

Most helpful comment

Can you provide answers to any of the questions in the issue template? It will help us figure out if there is a bug and if so how to go about fixing it.

All 5 comments

Can you provide answers to any of the questions in the issue template? It will help us figure out if there is a bug and if so how to go about fixing it.

@devsnek Sorry, I submitted empty issue by mistake. Details provided alread.

@fredZgred The relative order of setTimeout() and setImmediate() callbacks is not guaranteed in any way, because the two callback queues are checked at different points in time on the event loop.

For example, if you replace test() with setTimeout(test), you鈥檒l get a different ordering of the two functions.

Looks like event loop made a joke on me 馃槃I tested it many times in many combinations and it gave me always the same result. Thanks for explanation and sorry bothering you.

Closing as answered then. :-)

Was this page helpful?
0 / 5 - 0 ratings