I am running an HTTP GET request using the popular node-request module for each item in an array; then running a function at the last item of the array. My code goes like this:
const request = require('request');
var arr = ['John', 'Jane', 'Marry', 'Scarlet'];
var x = 0;
arr.every(function (i) {
x++;
/*instead of waiting for request, it adds x and moves on making x
3 when the first request finally runs*/
request({
url: 'http://localhost:8810/getLastName?firstName=' + i
}, function (error, response, body) {
//execute some code
if(x==arr.length){
//therefore this function is run for each item in the array.
// it should run only once per array.
}
})
return true;
})
I was hoping to achieve this without writing a ton of code therefore keeping my code nice and neat.
EDIT: I'm not trying to run it in order. I don't mind it running in parallel but I'm just trying to trigger a function once on completion.