In this article, we are going to further discuss what an Even loop means, how it works and what are the major considerations to keep in mind. So let’s get into it.
Let’s start explaining what asynchronous programming is and how it can change the approach to programming. The typical multithreaded application works with operations and every new operation will need a new thread to perform that operation in the OS kernel, of course, the real thing is a bit more complicated. So let’s take a real-life example.
Imagine you have to manage a restaurant. Let’s suppose the kitchen acts as a processor, waiters act as thread and orders act as operations that the processor has to process. If a waiter had to wait for every order to be done before starting to wait for another table that would mean that they can only serve as many tables as there are waiters at the restaurant. However, a classical restaurant works with an asynchronous approach. The waiter would leave instructions to the kitchen and would not wait for the order to be done and would take another order instead. After the order is done the waiter would be notified to serve the meal to the customer.
In programming, this would be relevant in terms of requests and responses. This means that the number of requests that an asynchronous approach can serve at the time is vastly superior to the synchronous approach.
Now that we know how asynchronous programming can change classical application flow, let’s take a look at how exactly the event loop works under the hood. There are three parts in the event loop that we need to know about including Call Stack, Event Queue/Callback Queue, WebAPIs (in the case of NodeJs, C++ APIs). We are going to talk about these parts down below.
How the Event Loop Works
Call stack holds all the synchronous function calls. Call stack will execute code in a last in first out approach. So last the most inner nested code will be executed first and then one level above that and so on. If the function call is asynchronous it will add a new call entry on WebAPIs and the API will add a new call to the callback queue after the asynchronous action is done.
Now we have two stacks with functions to be executed. This is where the Event Loop comes in. Event Loop’s job is to watch Callback Queue and Call Stack, whenever Call Stack is empty and we have called in Callback Queue. The event loop will move calls to Call Stack and execute them in succession.
What Should You Keep in Mind?
Frequently Asked Questions
How does the Event Loop work?
The event loop utilizes three major components, Call Stack, Event Queue, and WebAPIs/C++ APIs. It manages function calls in a way that asynchronous events would go into the event queue and will be called only after the call stack is empty.
Written by Keti Getiashvili
Based on an interview with Nika Jorjoliani