Is Javascript event queue a simple FIFO or not?

Take a look at this example:

function A() { console.log('A'); } function B() { console.log('B'); } // and then i setTimeout(fn, 0) both of them setTimeout(A, 0); setTimeout(B, 0); 

Is it guaranteed that B will start immediately after A ?
Is it possible for the browser to add another task to the queue between A and B ?

Note: None of the functions A or B add any new tasks to the event loop.

 var callbacks = []; // then add a bunch of callbacks ... (none adds events to event queue) //case 1: callbacks.forEach(cb => setTimeout(cb,0)) //case 2: setTimeout(function() { callbacks.forEach(cb => cb()); },0); 

Is there any difference in callback order in case 1 vs case 2 ?

+5
source share
4 answers

Is it guaranteed that B will work immediately after A?

Not right away, no, but it guarantees that B will work after A A full description is in the specification , but, in general, it is specifically considered in the timer initialization step , which, in particular, says:

Wait until any calls to this algorithm are completed that have the same method context that was run before and whose timeout is equal to or less than this.

Optionally, wait another user-defined user period.

Queue to task task.

... where the task is to call the call you gave setTimeout .

The queue task must be executed in order, and therefore A will execute before B in a compatible browser.

Note that this is guaranteed because they were queued by the same method context (see the specification for details on what this means).

Is it possible for the browser to add another task to the queue between A and B?

Yes. The browser can be multithreaded and queue a task for some other thing (message from a web worker, etc.) Between successive task queues for A and B In this case, you will see A run, then a handler for another task (processing a message from a web worker or something else), then B

+3
source

They will always shoot in the correct order, A , then B However, they may not work sequentially.

In fact, the specification says that timeout callbacks do not even get put in the execution queue until a timer expires, which may be subject to additional delays - the timeout you specify is just the minimum wait time.

+1
source

JavaScript uses an event queue for timers.

enter image description here

Also see this article: An excellent and still up-to-date article on John Resig's JavaScript timers.

+1
source

Yes, if you try this:

 function A() { console.log('A'); } function B() { console.log('B'); } function C() { while(1) { console.log('C'); } } // and then i setTimeout(fn, 0) both of them setTimeout(A, 0); setTimeout(C, 0); setTimeout(B, 0); 

You will see that the C function has an infinite loop, so the B function never executes. This is because the functions are transferred to the task / event list and are executed as final ones. This applies to the event list. SetTimeout has a special way of working. but if you ask.

On the other hand, if you ask whether this case will ever be executed first, and then B, the answer is not a guarantee ue, because these are not the only events that can be fired.

-1
source

Source: https://habr.com/ru/post/1242631/


All Articles