JavaScript Event Loop, Pushing Tin
The Event Loop is one of the important concepts in the Asynchronism world that every frontend developer must master. This is a simple one, everyone knows how a control tower interacts with the airplanes in the airports. The Event Loop and the I/O tasks work almost the same.
As a frontend developer I firmly believe that you must master JavaScript (commonly known as Vanilla JavaScript) before starting to learn any of its frameworks like Vue, Angular or React. If you dominate JavaScript, it will be easier to learn any framework for sure.
One important concept in the JavaScript world is the famous Event Loop system. Remember, although JavaScript is synchronous, it is complemented by an asynchronous non-blocking model in a single thread, which is implemented with an Event Loop.
The previous paragraph may sound very complex, just like handling the air traffic from the New York’s International Airport control tower. But don’t worry, you don’t have to know how it’s implemented at a low level.
You just need to understand the concept or the model in order to survive to any interview and even in your journey as frontend developer.
You don’t have to know in detail what’s inside a Swiss Watch, but you surely understand that there are a couple of pieces and gears working together to achieve a goal.
So, in a film context, we can simply say that the Event Loop is that Russell Bell who controls the way the airplanes in your code (I/O tasks) land on the browser.
CallStack: the landing track
The instructions in the code are sequentially pushed into this LIFO (Last In, First Out) pile, are executed one by one, and are finally removed from it.
It works as the landing track of the main or biggest airport in the area, where regular airplanes usually land, unloads their passengers and complete their trips.
Get David Moreno’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s see it in a small example:
function plane1(){
console.log("Plane 1 has landed");
}
function plane2(){
console.log("Plane 2 has landed");
}
function plane3(){
plane1();
console.log("Plane 3 has landed");
}
function main() {
plane2();
plane3();
}
main();
// Plane 2 has landed
// Plane 1 has landed
// Plane 3 has landedmain()is pushed into the CallStack.plane2()is pushed into the CallStack.- The
console.log()insideplane2()is pushed, executed and removed. plane2()is removed and thenplane3()is pushed.plane1()is pushed into the CallStack.- The
console.log()insideplane1()is pushed, executed and removed. plane1()is removed.- The
console.log()insideplane3()is pushed, executed and removed. plane3()is removed.- Finally,
main()is removed.
Callback Queue, wait until you can land
The previous code works well for normal synchronous tasks. But when it comes to asynchronous tasks, things change a bit.
When there are terrible weather conditions for an airplane or two or more airplanes arrive at the same destination at the same time, they must continue flying around until these situations are sorted, the landing track is clear and then the control tower grants them permission to land.
So, when an asynchronous task that was delegated to an external task runner is complete, its callback is pushed into the Queue. However, it must wait here until the Event Loop checks that the CallStack is clear and then that callback can be moved and executed there.
That’s why applications are able to guarantee that an asynchronous task like setTimeout(() => {…}, 3000) is going to take at least 3 seconds before it’s executed, but cannot guarantee that it’s going to take exactly 3 seconds. It could be more, depending on how busy the CallStack is.
There are some asynchronous patterns in JavaScript which take advantage of the Event Loop mechanism, like callbacks, promises, async/await and generators. You can find more information about these patterns in Medium.
I don’t want to conclude this article without pointing out a special mention for promises. They are often the result of interaction with important asynchronous APIs, and thus are an important part of what applications serve, we don’t want them to be delayed.
The ECMAScript standard describes the use of a special queue, called a Micro-task queue, with a higher priority dedicated to handling promise callbacks. This way, they are executed before any other non-promise callback.
And there we have it. I hope you have found this useful. Thank you for reading.
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.








