The Event Loop

To accomplish this, JavaScript maintains a single-threaded Event LoopWhen the Event Loop kicks off, it'll run our program. Following the execution of a piece of code (such as that which initiates our program), the Event Loop will await messages (or Events) indicating that something has occurred (for example, a network request has completed or a browser UI event has occurred). When it receives a message, it will then execute any code that is depending upon or listening for that Event. The Event Loop will, again, run that code to completion before continuing to await other messages. This process repeats infinitely until the JavaScript program is halted (for example, by closing a tab in a browser).

The fact that the Event Loop will always run a given piece of code to its completion means that any long-running or blocking code will prevent any other code from executing until it has completed. Some older browser API methods such as alert() and prompt() are examples of blocking functions that you may encounter. Calling these will effectively block any further execution of your JavaScript program:

alert('Hello!');
console.log('The alert has been dismissed by the user');

Here, console.log() will not be evaluated until the alert dialog is dismissed by the user. This could be milliseconds, minutes, or even hours. During this period, our JavaScript program is halted, unable to continue. Its Event Loop may be receiving Events but it will not run the code associated with those Events until alert() finally completes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.229.253