I'm running a for loop which calls a function that does a GET request
for (var i = 0; i < uids.length; i++) {
makeRequest(uids[i]);
}
function makeRequest(hostname) {
request({
url: url,
json: true
}, function(error, response, body) {
if (!error && response.statusCode === 200) {
//display the data etc
}
}
}
Is it possible for the for loop to ONLY iterate after each request is made and completed. eg once the GET request is complete and returns a result?
I thought about implementing some timeouts, however I am not sure that that is the best solution for this.
Make it recursive until you have items left:
var itemsLeft = uids.length;
function makeRequest(hostname) {
request({
url: url,
json: true
}, function(error, response, body) {
if (!error && response.statusCode === 200) {
if( itemsLeft !== 0 ) {
makeRequest( hostname );
itemsLeft -= 1;
}
}
})
}
Can suggest to take a look into something like https://github.com/FuturesJS/forEachAsync . Check their example to understand how to use the library.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.