What is Asynchronous programming in JavaScript ?

Rumman Ansari   Software Engineer     220 Share
☰ Table of Contents

Table of Content:


Asynchronous programming in JavaScript is a way of executing code that allows other tasks to run while a particular task is being executed. In other words, asynchronous programming allows you to run a task in the background while the main program continues to execute. This is particularly useful when working with tasks that may take a long time to complete, such as retrieving data from a server or performing complex calculations.

Asynchronous programming in JavaScript is typically achieved using callback functions, promises, and async/await. Callback functions are functions that are passed as arguments to other functions and are called once the main function has completed its task. Promises are a way of handling asynchronous operations and providing a clean interface for chaining together multiple asynchronous operations. Async/await is a newer syntax introduced in ECMAScript 2017 that provides a simpler way of working with promises.

By using asynchronous programming in JavaScript, you can create web applications that are more responsive and efficient, as well as make it easier to handle complex tasks without blocking the main thread. However, working with asynchronous code can be challenging, and it requires a good understanding of JavaScript's event loop and how asynchronous functions are executed.

Example of Asynchronous programming in JavaScript

One example of asynchronous programming in JavaScript is making an HTTP request to a server. The process of sending the request and waiting for a response can take a significant amount of time, during which the rest of the JavaScript code should continue to execute.

Here's an example using the fetch API to make an HTTP request asynchronously:


fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));

In this example, the fetch function sends an HTTP request to the specified URL and returns a Promise object. The then method is called on the returned Promise, which takes a callback function that is executed when the Promise is fulfilled with a successful response from the server. The catch method is called if the Promise is rejected due to an error in the request or response.

The callback function passed to the first then method receives the response object as an argument, which is then parsed as JSON using the json() method. The returned Promise from this method is then passed to the next then method, which takes a callback function that receives the parsed JSON data as an argument and logs it to the console.

Note that the main JavaScript thread can continue to execute other code while the HTTP request is being sent and while waiting for the response from the server. Once the response is received, the callback function passed to the first then method is executed, allowing the program to continue with the parsed data.