What the heck is the event loop anyway?

In Philip Roberts’ easy to understand lecture on how exactly JavaScript is working behind the scenes, and explaining the order of execution for the stack, task queue, event loop, as well as WEB APIs. It is great to have his 2 years of practice condensed into an uncomplicated lecture geared towards beginners and intermediates. He reaffirmed that JavaScript does in fact work one at a time through the call stack. He showed examples of bad news that can occur in the stack such as blowing the stack (creating an endless loop) and blocking the stack (having items with slow load time in the way of JS you want to execute in the stack). If call stack has slow data processing items it cant run any other code. That is where asynchronous call backs come in handy. setTimeout makes things vanish from the stack and appear back in, a simple concept but very helpful to see diagramed with his animated graphics. The broswer gives us web api’s that we can make calls to, setTimeout is an API provided by the browser. The browser kicks off a timer for you and takes it off the stack. An important note is that the web API can’t add things to your code. When setTimeout is used the item is taken off the stack and sent to the task cue which holds your call backs.

The graphic he used to explain the process.

The event loops job is to look at the stack and look at the task queue, If the stack is empty it takes the first thing on the queue and pushes it to the stack to be executed. Now a setTimeout set to ‘0’ allows you to clear the stack the same way as if it had time by allowing the stack to finish before pushing the task queued item to the end. This help illustrates that setTimeout is not a guaranteed time of execution. It is a MINIMUM time of execution.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.