---
title: "How can I return the response from an asynchronous call?"  
description: "How can I return the response from an asynchronous call?"  
author: "Utpal Vishwas"  
published: 2023-08-29  
updated: 2023-09-02  
canonical: https://www.mindstick.com/forum/159729/how-can-i-return-the-response-from-an-asynchronous-call  
category: "javascript"  
tags: ["javascript", "asynchronous"]  
reading_time: 2 minutes  

---

# How can I return the response from an asynchronous call?

How can I return the [response](https://www.mindstick.com/forum/12719/how-to-make-response-write-display-special-character-like-lt-gt) from an [asynchronous](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) call?

## Replies

### Reply by Aryan Kumar

There are a few different ways to return the response from an asynchronous call. One way is to use a promise. A promise is an object that represents the eventual completion or failure of an asynchronous operation. You can use promises to chain together asynchronous operations and to handle errors.

The following code shows how to return the response from an asynchronous call using a promise:

```plaintext
function getUsers() {
  return new Promise(function(resolve, reject) {
    // Make the asynchronous call.
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/api/users");
    xhr.onload = function() {
      if (xhr.status === 200) {
        resolve(JSON.parse(xhr.responseText));
      } else {
        reject(new Error(xhr.statusText));
      }
    };
    xhr.send();
  });
}
```

In this code, the `getUsers()` function returns a promise. The `getUsers()` function makes an asynchronous call to the `/api/users` endpoint. The `getUsers()` function then resolves the promise with the response data if the call is successful, or rejects the promise with an error if the call is not successful.

Another way to return the response from an asynchronous call is to use an async function. An async function is a function that can be used to perform asynchronous operations. Async functions use promises internally to handle the asynchronous operations.

The following code shows how to return the response from an asynchronous call using an async function:

```plaintext
async function getUsers() {
  // Make the asynchronous call.
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "/api/users");
  xhr.onload = function() {
    if (xhr.status === 200) {
      return JSON.parse(xhr.responseText);
    } else {
      throw new Error(xhr.statusText);
    }
  };
  xhr.send();
}
```

In this code, the `getUsers()` function is an async function. The `getUsers()` function makes an asynchronous call to the `/api/users` endpoint. The `getUsers()` function then returns the response data if the call is successful, or throws an error if the call is not successful.


---

Original Source: https://www.mindstick.com/forum/159729/how-can-i-return-the-response-from-an-asynchronous-call

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
