Javascript: Asynchronous Programming

Bharath Kumar Murugan
2 min readApr 5, 2023

Asynchronous programming is an important concept in JavaScript that allows you to execute multiple tasks simultaneously. This is done by using asynchronous functions, which allow you to start a task and continue executing other tasks while waiting for the result of the first task.

To better understand how asynchronous programming works in JavaScript, let’s consider the following example:

function fetchData(url) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Data received from ${url}`);
}, 2000);
});
}

console.log("Before calling fetchData");
fetchData("https://example.com")
.then(data => console.log(data))
.catch(error => console.error(error));
console.log("After calling fetchData");

In this example, we define a function fetchData that returns a Promise that resolves after a delay of 2 seconds with a message containing the URL from which the data was fetched. Then, we call fetchData with the URL "https://example.com" and log a message before and after calling the function.

If we run this code, we will see the following output:

Before calling fetchData
After calling fetchData
Data received from https://example.com

As you can see, the message “Before calling fetchData” is logged first, followed by the message “After calling fetchData”. However, the message “Data received from https://example.com" is logged after a delay of 2 seconds. This is because the fetchData function is asynchronous, and it returns a Promise that is resolved after a delay.

By using asynchronous functions like Promises, you can execute multiple tasks simultaneously without blocking the main thread of execution. This allows your JavaScript code to be more efficient and responsive, especially when dealing with network requests or other time-consuming operations.

Here’s another example that demonstrates how to use asynchronous programming to fetch data from multiple sources:

function fetchAllData(urls) {
const promises = urls.map(url => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Data received from ${url}`);
}, 2000);
});
});

return Promise.all(promises);
}

console.log("Before calling fetchAllData");
fetchAllData(["https://example.com", "https://google.com", "https://facebook.com"])
.then(data => console.log(data))
.catch(error => console.error(error));
console.log("After calling fetchAllData");

In this example, we define a function fetchAllData that takes an array of URLs and returns a Promise that resolves with an array of messages containing the URLs from which the data was fetched. We use the map function to create an array of Promises that are resolved after a delay of 2 seconds, and we use the Promise.all function to wait for all the Promises to resolve before returning the result.

If we run this code, we will see the following output:

Before calling fetchAllData
After calling fetchAllData
[
"Data received from https://example.com",
"Data received from https://google.com",
"Data received from https://facebook.com"
]

As you can see, we can fetch data from multiple sources simultaneously and wait for all the results to be returned before continuing with our code.

In summary, asynchronous programming in JavaScript is a powerful feature that allows you to execute multiple tasks simultaneously without blocking the main thread of execution. By using asynchronous functions like Promises, you can write more efficient and responsive code, especially when dealing with network requests or other time-consuming operations.

--

--

Bharath Kumar Murugan
0 Followers

FrontEnd Developer | Javascript Enthusiast | Linux | Docker