---
title: "How to Return a Value from an Asynchronous Function Call in JavaScript?"
description: "JavaScript's asynchronous nature can cause issues when you need to return a value from a function that involves asynchronous operations. When working with…"
url: https://www.danny.engineering/answer/javascript/how-to-return-a-value-from-an-asynchronous-function-call-in-javascript
author: Danny
published: 2024-08-15T08:19:00.000Z
updated: 2024-08-15T08:19:00.000Z
tags: ["Javascript"]
---

# How to Return a Value from an Asynchronous Function Call in JavaScript?

## Problem


JavaScript's asynchronous nature can cause issues when you need to return a value from a function that involves asynchronous operations. .


When working with asynchronous operations in JavaScript, such as fetching data from an API or reading a file, it can be tricky to return a value from a function. Let's start by looking at an example that highlights the problem


```javascript
function getData() {
  setTimeout(() => {
    return "Data fetched!";
  }, 1000);
}

const result = getData();
console.log(result); // Output: undefined
```


Here we have defined a function, `getData()` which is intended to return the string `"Data fetched!"` after a delay of 1 second. However, when you try to log `result`, you get `undefined` instead of the expected string.


This happens because `setTimeout` is asynchronous. The function `getData()` returns `undefined` before the callback inside `setTimeout` is executed. To correctly return the value, you need to handle asynchronous behaviour properly.


## Solution


Returning a value from an asynchronous function in JavaScript requires careful handling due to the non-blocking nature of the language. Using callbacks, Promises, or `async/await` allows you to work with asynchronous operations effectively. Each method has its use cases, and choosing the right one depends on the complexity of your code and your specific requirements.


To properly return a value from an asynchronous function, you should use one of the following methods:


### **Callbacks**


You can pass a callback function to handle the result when it's ready.


```javascript
function getData(callback) {
  setTimeout(() => {
    const data = "Data fetched!";
    callback(data);
  }, 1000);
}

getData((result) => {
  console.log(result); // Output: "Data fetched!" after 1 second
});
```


### **Promises**


Promises provide a more modern approach to handling asynchronous operations and returning values.


```javascript
function getData() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Data fetched!");
    }, 1000);
  });
}

getData().then((result) => {
  console.log(result); // Output: "Data fetched!" after 1 second
});
```


### **async/await**


`async/await` syntax is built on top of Promises and offers a cleaner and more readable way to work with asynchronous code.


```javascript
async function getData() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Data fetched!");
    }, 1000);
  });
}

async function fetchData() {
  const result = await getData();
  console.log(result); // Output: "Data fetched!" after 1 second
}

fetchData();
```
