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
source share