I have a problem with looping get request to apis in nodejs. I want to fetch data from multiple endpoints and continue when all request are done. I tried something like this but its running async and logging an empty array. Any tips how know for sure when all requests are ready?
var api_endpoints = { "1": "url1", "2": "url2", "3": "url3" };
var allApiSources = [];
_.each(api_endpoints, function (val, key) {
request(val, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
_.each(data.url, function (val, key) {
allApiSources.push(value);
});
}
});
});
console.log(allApiSources); // []
Thanks!