I want to make n amount of api calls and add the all the results to an array. Then return the array.
n = length of word array
It is returning an empty result. I know it's an async function but for the life of me I can't figure out a solution, any help would be appreciated.
app.get('/api/', async (req, res) => {
let wordArray = ["word1", "word2", "word3"]
let resultArray = []
for (let i = 0; i < wordArray.length; i++) {
fetch('apiurl' + new URLSearchParams({
word: wordArray[i],
}))
.then(res => res.json())
.then((responseData) => {
resultArray.push(responseData);
})
.catch(error => console.log(error));
}
console.log(resultArray);
});
Promise.all()to get all the results, then sum that.