2

I got confused when I read that when I set the (same) time in 2 timeSetout methods, the order in which the function will be called cannot always be predicted (there was not reasoning provided).

setTimeout(()=> console.log("first"), 5000);
setTimeout(()=> console.log("second"), 5000); 

As I understand, the timesetouts will be added to the event table, which keeps track of the events (in this case the event is the time), then it sends it to the event queue, which stores the order in which the functions should be executed, then event loop checks and sends those functions to the call stack.

Since the first timesetout (logging "first") is being added FIRST to the event table, I would imagine it being the FIRST to be send to the event queue (also considering the event queue is a stak data structure) and the FIRST to be send to the call stack during the event loop.

I couldnt find anything about that online, also not reasons as to why it could be executed in random order and would be happy about an answer!

Also, on a side note: Could someone please explain when race conditions could be possible as if the above is not true, I cannot think of any case since javascript is single threaded. Thanks!!

javascripting
  • 1,135
  • 4
  • 25
  • 46
  • I believe the IO Event loop can interfere with ordering. For e.g., it evaluates FIRST at 4999 ms, at 5000ms it (JS runtime) receives a new input, and then evaluates SECOND at 5001ms causing SECOND to be evaluated first. – Loveen Dyall Oct 01 '18 at 10:32
  • Why setTimeout() should have any order at all? The calls are independent. – Sergej Oct 01 '18 at 10:58
  • 1
    I was unable to reproduce case when "second" is output before "first". Also I have never heard about misordering deferred calls. Does you sample code shown different results for you? – skyboyer Oct 01 '18 at 11:02
  • Where did you read that the order cannot be predicted? Please quote the book or link the website. I definitely cannot reproduce this, I always get `"First"` first. – Bergi Oct 01 '18 at 13:00
  • I cannot remember where I saw this, but it was on stackoverflow. I also cannot reproduce it, but my question is more about if in javascript its garantied that this wouldnt happen and if theres a reasoning and if not why not. – javascripting Oct 01 '18 at 13:34

1 Answers1

1

The specification states the following (emphasis added):

  1. If method context is a Window object, wait until the Document associated with method context has been fully active for a further timeout milliseconds (not necessarily consecutively). Otherwise, method context is a WorkerGlobalScope object; wait until timeout milliseconds have passed with the worker not suspended (not necessarily consecutively).

  2. Wait until any invocations of this algorithm that had the same method context, that started before this one, and whose timeout is equal to or less than this one’s, have completed.

As far as I understand this should ensure the execution to be in order, and you will always see first and then second.

Community
  • 1
  • 1
likle
  • 1,717
  • 8
  • 10