this is my first question in this platform, I hope you can help me out, I have a doubt.
How to put fetch API data (async/await) inside a global scope Array? | Vanilla Javascript
I have this variable called storeItems that is initially empty:
let storeItems = [];
And I created this function called fetchApi to call the data from the API and push it in to the storeItems array, which is declared outside the async function.
async function fetchApi() {
const dataApi = await fetch('https://fakestoreapi.com/products')
const dataJason = await dataApi.json()
dataJason.map((element) => {
storeItems.push(element)
})
showProducts();
}
fetchApi();
It partially works, it only works when i execute the showProducts function inside the fetchApi function.
If I try to access the array properties of storeItems outside the fetchApi I will get undefined. If I console log the array I can see the data in the log, but cannot access to any of the objects inside the array, I get undefined.
Thanks.