avatarDr. Ashish Bamania

Summary

The provided web content explains the JavaScript event loop, callback queue, and their interaction with the JavaScript engine and Web APIs to facilitate asynchronous operations in a single-threaded environment.

Abstract

The article "JavaScript: Event Loop & Callback Queue Explained" delves into the core components that enable JavaScript's asynchronous capabilities within a browser environment. It describes the JavaScript engine, which includes the call stack and memory heap, and emphasizes the importance of the event loop and callback queue in managing asynchronous operations. The event loop checks for an empty call stack to execute callback functions from the queue, giving the illusion of multi-tasking in this single-threaded language. The Web API, provided by browsers, includes functions like setTimeout() and setInterval(), which are not part of the JavaScript engine but are crucial for executing asynchronous code. The article uses an example with three functions to illustrate the sequence of execution and how the event loop, callback queue, and Web APIs work together to handle asynchronous tasks.

Opinions

  • The author suggests that understanding the event loop is essential for grasping JavaScript's asynchronous nature, which is key to building responsive web applications.
  • The article implies that the event loop is a sophisticated mechanism, referred to as the "secret sauce," that allows JavaScript to perform asynchronous operations without being inherently multi-threaded.
  • The FIFO (first-in-first-out) principle of the callback queue is highlighted as a critical aspect of maintaining the order of execution for asynchronous callbacks.
  • The author provides external resources for readers seeking deeper knowledge about the JavaScript engine and Web APIs, indicating a commitment to comprehensive learning.
  • The use of visual examples and analogies, such as the call stack and callback queue, is intended to simplify complex concepts for the reader.
  • The article encourages reader interaction by inviting them to follow along with the provided code examples and by suggesting further reading through the author's books on Python and Artificial Intelligence.
  • The author expresses gratitude towards the readers and seeks engagement in the form of claps and follows on Medium, indicating the importance of community feedback and support for content creators.

JavaScript: Event Loop & Callback Queue Explained

Photo by Claudio Schwarz on Unsplash

When JavaScript is run on a browser, the following components come into play:

  • The JavaScript Engine with its Call stack and its Memory heap
  • The Event Loop
  • The Callback Queue
  • The Web API

JavaScript Engine

To read about this, check out the article here.

Event Loop

Remember that:

JavaScript is a single-threaded language.

The Event-loop is the secret sauce that helps give JavaScript its multi-tasking abilities (almost!).

This loop constantly checks whether the call stack is empty or not and if it is, the functions waiting to be executed in the callback queue get pushed to the call stack.

Finding it tough to understand? Don’t worry! It will be much easier to understand with the example below.

Let’s move on to the next component before that.

Photo by Jexo on Unsplash

Callback Queue

The Callback queue is a data structure that operates on the FIFO (first-in-first-out) principle.

Callback functions that need to be asynchronously executed, are pushed onto the callback queue.

These are later pushed to the Call stack to be executed (when the event loop finds an empty call stack).

Web API

These are different APIs provided by the web browser that helps you build web applications.

A few that you might be familiar with are:

  • setTimeout()
  • setInterval()
  • clearTimeout()
  • clearInterval()
  • fetch()

Remember that these are not in-built into the JavaScript engine.

Read more about Web APIs here: https://developer.mozilla.org/en-US/docs/Web/API

Photo by Paul Esch-Laurent on Unsplash

How Do These Components Work Together?

We will write 3 functions to understand this.

function print_first_statement(){
    console.log("First statement");
};
function print_second_statement(){
    setTimeout(() => {
    console.log("Second statement");
}, 3000);
};
function print_third_statement(){
    console.log("Third statement");
};

These can be executed as follows:

print_first_statement();
print_second_statement();
print_third_statement();

This outputs to:

Output:
First statement
Third statement
Second statement

Did you notice the delay in printing Second statement?

Let’s see how this works in the background:

1

  • print_first_statement() is called, its function execution context is placed in the call stack and the function is executed
  • This outputs to printing First statement on the screen
  • print_first_statement() function execution context is popped off the call stack

2

  • print_second_statement() is called, its function execution context is placed in the call stack and the function is executed
  • This creates a call to the WebAPI (due to the setTimeout() function)
  • Once WebAPI is done executing this (for 3000ms), it places the anonymous callback function in the Callback queue.
  • This will be pushed to the call stack once it is found to be empty by the Event loop
  • print_second_statement() function execution context is popped off the call stack

3

  • print_third_statement() is called, its function execution context is placed in the call stack and the function is executed
  • This outputs to printing Third statement on the screen
  • print_third_statement() function execution context is popped off the call stack
  • When the call stack becomes empty, the anonymous callback function in the callback queue will be pushed into this empty call stack by the Event loop
  • This callback function’s function execution context is placed in the call stack and the function is executed
  • This outputs to printing Second statement on the screen
  • The callback function’s function execution context is popped off the call stack

If you liked this article, please click on the clap button (one user can click up to 50 times) and follow me on Medium.

Thanks for reading this article!

If you are someone new to Python or Machine Learning, check out my books below:

JavaScript
Node
Programming
Software Development
Javascript Tips
Recommended from ReadMedium