I wrote this async function that fetches data from an API.
const getData = async () => {
const response = await fetch(`${url}`);
const json = await response.json();
return json.results.slice(0,3);
}
I expect it to return an array containing 3 objects.
I call the getData
function in a non-async function
function App() {
const thing = getData().then(data => data);
console.log("Thing is: " + thing);
}
But instead of my array being printed out I'm getting "Thing is: [object Promise]"
and I don't know why. I've read around and it says that async
functions will always return a promise, so although my return statement in getData
defines an array of 3 objects it will ultimately return a promise that is once resolved I can access that array. So I go ahead and then use .then
when I call the getData
function, but still I'm getting the promise instead of the data? I think I'm doing something wrong with the arrow function, I don't know if I'm returning the data correctly.