Introduction
JavaScript is a single-threaded language that can execute only one operation at a time. However, JavaScript is also known for its non-blocking, asynchronous nature, which allows it to handle multiple tasks simultaneously without blocking the main thread. This is made possible by the Concurrency Model and the Event Loop. In this article, we'll explore this concept, explain how it works, and provide real-life examples to illustrate its importance.
Understand conventions in JavaScript
** Concurrency ** refers to the ability of a system to handle multiple tasks simultaneously. In JavaScript, this convention is achieved through Event Loop and Call Queue.
Event loop
Event loops are the cornerstone of JavaScript's concurrency model. It checks two structures sequentially: Call Point and Call Queue.
Callpoint : Think of callpoints as function calls. When a function is called, it is pushed onto the stack and popped off the stack when the value is returned. JavaScript processes one function call at a time from the top of the stack.
Call Queue : Asynchronous operations such as getting data from the server or reading files are handled by the browser or the Node.js runtime. After this process is completed, the appropriate callback function is placed in the callback queue.
The Event Loop continuously checks the Call Point and Call Queue. When the callpoint is idle (ie, no function is currently being executed), the Event Looper retrieves the callback function from the Call queue and clicks the Callpoint to execute it.
Real Life Example: Retrieving data from an API
Let's illustrate the Concurrency Model and Event Loop with a practical example - retrieving data from an API.
// Simulate the API request function
fetchDataFromAPI() function
setTimeout (() => {
console.log("Data received from API");
}, 2000);
}
console.log("Starting to get information");
fetchDataFromAPI();
console.log("Getting information");
In this example, we have a fetchDataFromAPI function that simulates making an asynchronous API request using setTimeout. It takes 2 seconds to complete the request. Meanwhile, we have console.log statements before and after the fetchDataFromAPI call.
Here's what happened:
- Get information is written to the console.
- fetchDataFromAPI is called, which starts the API request, but the JavaScript engine does not wait until it is finished. Instead, it moves to the next line.
- The end of data acquisition is written to the console.
- After 2 seconds, when the API request is completed, "Data received from API" is logged.
This may be due to behavioral phenomena. While an API request is in progress, the JavaScript engine continues to perform other tasks. After the API request completes, the callback function is placed in the Call queue. The event loop accepts this callback and pushes it to the Callpoint to execute when the Callpoint is empty.
The results
The Concurrency and Event Loop model in JavaScript enables asynchronous programming, allowing tasks to be executed simultaneously without blocking the main thread. This is very important for building efficient web applications and efficient I/O operations.
Understanding these concepts is essential for JavaScript developers to write safe, non-blocking code that can handle multiple tasks seamlessly, making web applications easier for users.
Top comments (1)
Nice 👍 love it it’s enforcing knowledge I knew I had but it gets better !! I didn’t know it was called concurrency model.
Thanx.
Would you consider this sort of knowledge intermediary; beginner or expert information ?!?