I need some help, I went to almost all questions here and in MDN and I couldn't find a solution for my problem. For sure it's here somewhere but I couldn't see it.
const fetch = require("node-fetch");
const url = 'https://myapi.com'
const getData = async () => {
const data = await fetch(url);
const dataStage1 = await data.json();
let finalData = dataStage1.data[0];
return finalData;
};
const data = getData();
console.log(data)
output:
Promise { <pending> }
I can't just const data = getData();
because I get SyntaxError: await is only valid in async function
I really don't understand what I'm doing wrong, in getData()
function I'm waiting for the results and returning the object I need.
I need to have this object in a variable because I need to manipulate it.
Thank you o much for any help explanation, I'm having a really hard time to understand why this is happening