Mastering the JavaScript Event Loop: Understanding and Using Asynchronous Code

Yonatan Merkebu
3 min readJan 25, 2023

JavaScript is a single-threaded programming language, which means it can only process one task at a time. However, it can handle many tasks at once by using an event loop.

The event loop is a mechanism that allows JavaScript to execute non-blocking code in a synchronous manner. It works by placing all asynchronous tasks in a message queue and processing them one at a time in a loop. This allows the JavaScript engine to continue running other code while it waits for the asynchronous tasks to complete.

Let’s take a look at an example to understand how the event loop works in JavaScript.

console.log("Start");

setTimeout(() => {
console.log("Timeout 1");
}, 1000);

setTimeout(() => {
console.log("Timeout 2");
}, 0);

console.log("End");

In this example, we have two setTimeout functions, one with a delay of 1000 milliseconds and the other with a delay of 0 milliseconds. When the code is executed, the event loop will start with the first task, which is to log "Start" to the console.

Next, the event loop encounters the first setTimeout function. It creates a timer and adds the callback function to the message queue. The event loop continues to the next task, which is the second setTimeout function. It creates another timer and adds the callback function to the message queue.

Finally, the event loop encounters the last task, which is to log “End” to the console. Once the task is completed, the event loop will check the message queue for any pending tasks.

In this case, it finds the second setTimeout function with a delay of 0 milliseconds. The callback function is moved to the call stack and executed, logging "Timeout 2" to the console.

The event loop then checks the message queue again and finds the first setTimeout function with a delay of 1000 milliseconds. Since the delay has not yet expired, the event loop will continue to check the message queue until the delay is over. Once the delay is over, the callback function is moved to the call stack and executed, logging "Timeout 1" to the console.

As you can see, the event loop allows JavaScript to execute the code in a synchronous manner while handling the asynchronous tasks in the background. This allows the JavaScript engine to continue running other code while it waits for the asynchronous tasks to complete.

It’s also worth noting that the event loop does not only handle setTimeout and setInterval functions but also for any asynchronous operation like fetch, click events, and more.

In conclusion, the event loop is a powerful mechanism that allows JavaScript to handle multiple tasks simultaneously. By placing asynchronous tasks in a message queue, the event loop allows the JavaScript engine to continue running other code while it waits for the asynchronous tasks to complete. This is a fundamental concept in JavaScript that allows developers to create responsive and efficient web applications.

--

--