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?