As a rule, except in special cases - JavaScript is executed synchronously and in order.
setTimeout(executeOtherCode, 5000)
Says "After 5 seconds, run the executeOtherCode function executeOtherCode " It continues to run the loop; it does not perform a “lock”.
After 5 seconds, the event loop will notice that the timer (well, length timers) has been configured and will execute them (one after the other).
If you want the functions to be executed in a 5 second delay of each other, you need to tell the next function to execute 5 seconds after the last one is completed, this template is called an asynchronous semaphore.
The general rule is that if it performs I / O, it must be asynchronous , so AJAX is asynchronous (as well as other HTTP requests, such as a script tag) and the interaction of an event is asynchronous (JavaScript responds to clicks, for example, he does not wait for them). Timers (setTimeout and setInterval are also asynchronous).
Everything else is synchronous.
Now some functions can use these other functions, but there is no silver bullet in determining what is. Just clear the documentation. Most asynchronous functions have a callback parameter (for example, the first executeOtherCode parameter), but some do not, and some functions accept callbacks, but are not asynchronous.
source share