I am trying to make multiple API calls based on the information in an array. For example, I have an array of ['London', 'New York', 'Mexico', 'China', 'Tokyo']
I want to get all the weathers using open weather api. I am trying to use promise.all. I need all the data back before rendering to the front, but my code isn't working.
let cities = ['London', 'New York', 'Mexico', 'China', 'Tokyo']
let promisesArray = []
return new Promise(function(resolve, reject) {
for(let i = 0; i < cities.length; i++) {
let cities = ['London', 'New York', 'Mexico', 'China', 'Tokyo']
let url = 'api.openweathermap.org/data/2.5/weather?q=' + cities[i]
request(url, function(error, response, body){
if (err) {
reject(error);
}
var data = JSON.parse(body)
var results = data.Search;
promisesArray.push(resolve(results));
});
}
})
Promise.all(promisesArray)
.then(function(results) {
})
.catch(function(error) {
})