I want to add a description property to each object in the locations array that is fetched from Wikimedia API but when I log its value inside the loop it is there, but outside the loop, it gets erased.
I looked for solutions with async/await functions or Promise.all() but it didn't work out.
Is there a way to store the value properly to access it later ??
let locations = [
{
latLng: [33.975561111111,28.555830555556],
name: 'Saint Catherine\'s Monastery',
searchTerm: 'Saint Catherine\'s Monastery',
urlSerchTerm: 'Saint%20Catherine\'s%20Monastery'
},
{
latLng: [29.91667,31.2],
name: 'Bibliotheca Alexandrina',
searchTerm: 'Bibliotheca Alexandrina',
urlSerchTerm: 'Bibliotheca%20Alexandrina'
}
];
async function fetchAsync (site, location) {
// await response of fetch call
let response = await fetch(site);
// only proceed once promise is resolved
let data = await response.json();
// only proceed once second promise is resolved
location.description = data[2][0];
return location.description;
}
// let fetches = [];
for (let i = 0; i < locations.length; i++) {
let site = `https://en.wikipedia.org/w/api.php?action=opensearch&search=${locations[i].urlSerchTerm}&limit=1&namespace=0&format=json&origin=*`;
fetchAsync(site, locations[i])
}
console.log(locations[1].description)