Learn React in 100 Days

Learning a new coding language can be daunting, stressful, and lonely. How would I know? Well, in 2015, I attended a 9-week coding bootcamp and learned Ruby on Rails. When I went on interviews, no…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Understanding the Event Loop in Javascript

Javascript is single-threaded language, meaning it runs one command at a time. How then does it not freeze when performing different async operation? And what makes it able to flow continuously through its program?

The answer is found in the Event Loop.

Here is a high-level overview of how the Event Loop works.

Javascript has a one, single Call Stack which tracks which function is currently executing and what functions need execute after that. The call stack operates on a Last In, First Out basis. Functions are added to the back and only the last item can be removed.

But what happens if a dev makes a request or puts a timeout on something? Shouldn’t the Call Stack freeze, waiting on the function to return before it can call the subsequent commands?

In theory, yes — but, because of the Event Table and Event Queue, Javascript is able to keep on ticking.

The Event Table is a data structure that tracks every setTimeout function or async operation in your program. It tracks which functions should be called after specific events. When that event occurs, the Event Table sends a signal and that correct function is moved to the Event Queue — another data structure integral to this whole thing.

The Event Queue is similar to the call stack, yet another LIFO stack. It receives functions form the Event Table and uses the Event Loop to send them to the Call Stack.

Finally, the Event Loop is what keeps this all humming along. It is a process constantly on the run, looking for commands to execute. First it checks the Call Stack and then moves to the Event Queue. Think of it as a productivity nerd chasing after inbox zero. When something needs its attention in the Call Stack — the main inbox, say — it executes it, and when there’s nothing in the call stack left to read, it moves to the Event Queue, moving things to the Call Stack to then be executed.

There’s a lot more to this obviously, but should be a good working definition for, say, an intermediate Javascript interview.

Add a comment

Related posts:

My Elementary School Blues

Whenever I remind fellow colleagues at my high school about the elementary school that some of us formally attended, I am usually met with utter shock when I disclose the fact that I hated our…

7 Steps to Personal Success for Women Who Refuse to Be Failures

In my personal and professional experience, I have been taught a plethora of techniques that have contributed to my personal success and that of my coaching clients. Everyone is different, and what…

10 Reasons Why You Should Stick with Your Weight Loss Goals

Weight loss is supposed to be hard. It requires time, effort, and an attitude of stick-to-it-ness. Falling short of these requirements can lead to failure once you hit bumps and plateaus along the…