There are a couple ways you can do this. Since all the calls to request()
are asynchronous and thus will finish sometime in the future long after your loop has finished, you will have to keep track of them somehow. I'll show two methods, one using Promises and one using a counter.
Promises
// create wrapper function that returns a promise
function requestPromise(options) {
return new Promise(function(resolve, reject) {
request(options, function(error, response, body) {
if (error) return reject(error);
resolve({response: response, body: body});
});
});
}
var arr = ["A" , "B", "C" , "D" , "E"];
Promise.all(arr.map(function(item) {
// create options object here for each request
var options = {
method: 'GET',
url:"some_url",
headers: {...}
};
return requestPromise(options);
})).then(function(results) {
// process results here
// call next() here because all processing is now done
next();
}).catch(function(err) {
// error happened
next(err);
});
Manual Counter
var arr = ["A" , "B", "C" , "D" , "E"];
var errDone = false;
var cntr = 0;
arr.forEach(function (arrayItem) {
var options = {
method: 'GET',
url:"some_url",
headers: {...}
};
request(options, function (error, response, body) {
if (error) {
if (!errDone) {
// latch error so we don't call next(error) multiple times
errDone = true;
next(error);
}
} else {
// process result here
// check if this is the last response
++cntr;
if (cntr === arr.length) {
// all responses done here
next();
}
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…