有幾個方法可以做到這一點。由於所有對request()
的調用都是異步的,因此在循環結束後很長一段時間內會完成,因此您必須以某種方式跟蹤它們。我將展示兩種方法,一種使用Promise,另一種使用計數器。
承諾
// 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);
});
手動計數器
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();
}
}
});
嗨@ jfriend00--非常感謝,它的確幫助我理解了這個對我來說完全陌生的概念。 只是關於你的解決方案的一個小問題 - 在catch()方法上,錯誤會包括失敗請求的具體響應,以及它的所有細節(所以我會知道哪個請求失敗),還是隻包含錯誤的迴應消息? –
@GadiBenAmram - 它將包含'request()'中的'error'值,它首先失敗。如果你願意,你可以包含更多的信息,只需構建自己的對象並添加更多信息,並調用'reject(yourObj)'而不是'reject(error)'。 – jfriend00
明白了!再次感謝@ jfriend00,它確實幫助:-)。 –