1

I've tried several ways to return an object from an async function. I'm quite new to JavaScript, and I can't see why this does not work.

The function uses an API to return some financial data, then after it gets the response it maps the returned Object into another object, which I want to return.

If I console.log the object, everything is ok. But having a lot of numbers in my terminal is not really what I hope to achieve.

I've tried the following:

let someObject = fetchData(url);
console.log(someObject)

Gives me "Promise { }"


Another try:

const returnFetchData = async() => {
    const objectToReturn = await fetchData(url);
    return objectToReturn;
}


console.log(returnFetchData);

Gives me "[AsyncFunction: returnFetchData]"

Heres my code:

async function fetchData(urlLink){
const res = await fetch(urlLink);
const out = await res.json();
const timeSeries = out['Time Series (Daily)'];

let returnObject = {
    dates: [],
    adjustedClose: []
}


Object.entries(timeSeries).forEach(
    ([key, value]) => {
    returnObject.dates.push(key)
    returnObject.adjustedClose.push(value['5. adjusted close'])
    }
);


return returnObject;

}

Edit: I want to return the array from returnObjects.adjustedClose.

All solutions so far returns a promise. This array should be used in another function, should I therefor map it in that function?

Dansekongen
  • 328
  • 3
  • 11
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – TKoL Mar 17 '20 at 16:50
  • 1
    async functions return promises. You can use the `.then` callback of a promise eg `promise.then(function(result){...})`, or, if you're inside an async function, `await` the promise, `var result = await promise;` – TKoL Mar 17 '20 at 16:51

2 Answers2

0

In your code

const returnFetchData = async() => {
    const objectToReturn = await fetchData(url);
    return objectToReturn;
}

console.log(returnFetchData); 

Swap out the console.log(returnFetchData); with console.log(await returnFetchData());

You must await an async function.

Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29
  • Returns [AsyncFunction: returnFetchData]. – Dansekongen Mar 17 '20 at 17:44
  • Did you keep the brackets? `await returnFetchData()` – Luke Garrigan Mar 18 '20 at 09:55
  • I tried to Await the function. But I want to return the arrays so I can use the data in another function. So another function should create const someArrays = await returnFetchData(); then use someArrays.dates. Maybe it should be in the main function? – Dansekongen Mar 18 '20 at 09:59
  • Can you show the code for the `fetchData` function? It does seem a little pointless to have a function called `returnFetchData` which just calls `fetchData` and doesn't do any processing – Luke Garrigan Mar 18 '20 at 10:02
  • In my question the code starts with “async function fetchData” which is the function. Any help is greatly appreciated, can’t find an answer to this anywhere. – Dansekongen Mar 18 '20 at 10:05
0

You can not return the data as it's not ready immediately. The data you want to return depends on some requests, so it needs some time to be prepared. To sum up you have to await that function, fetchData, to fetch and prepare data, then it will be ready to return the data you want.

Consequently, you can reach the array you desire, returnObject.adjustedClose, by blocking like this:

let desiredData = await fetchData(urlLink);
let desiredArray = desiredData.adjustedClose;
console.log(desiredArray);

or non-blocking like this:

fetchData(urlLink).then((desiredData) => {
  let desiredArray = desiredData.adjustedClose;
  console.log(desiredArray);
});

Keep in mind that these both will not log the desiredData immediately, it will take some time to fetch data from the urlLink and to prepare it.

State one option of these lines of code, wait some seconds and see the result in the console.

hasany
  • 157
  • 2
  • 9