I am using promises and async to wait for a web request to finish. The variable info is empty when displaying its value using console.log(infos);
I am unsure as to why and what I am doing wrong?
var processFruits = async () => {
let infos = [];
let fruits = ["apples", "pears", "bananas"]
await fruits.forEach(async fruit => {
let response = await getFruitInfo(fruit);
infos.concat(response);
console.log(response);
});
console.log(infos);
};
let getFruitInfo = (fruit) => new Promise((resolve, reject) => {
fetch("https://www.fruitmap.org/api/trees", {
method: 'GET',
}).then(response => {
if (response.ok) {
resolve(response.json());
} else {
reject(error);
}
}).catch(error => {
reject(error);
});
});