我使用async.queue来调用microsoft graph API。只要代码进入if(错误)块,队列就不会耗尽。如果(错误)块进入读取ECONNRESET,则几乎总是发生错误。 请在下面找到代码:
function asyncDataPull(url, callback) {
var deferred = Q.defer();
request.get({
url: url,
headers: {
'content-type': 'application/json',
authorization: 'Bearer ' + token,
displayName: getName(url)
}
}, function(err, response, body) {
if (err) {
callback(err);
}
else if (!body) {
callback();
}
else if(typeof(JSON.parse(body)['error']) != "undefined") {
callback();
} else if(typeof(JSON.parse(body).value) != "undefined"){
callback();
}
else{
callback();
}
})
}
var q = async.queue(asyncDataPull, 100);
for (var userIndex = 0; userIndex < users.length; userIndex++) {
q.push('https://graph.microsoft.com/v1.0/users/' + users[userIndex].mail + '/messages');
q.push('https://graph.microsoft.com/v1.0/users/' + users[userIndex].mail + '/events');
q.push('https://graph.microsoft.com/v1.0/users/' + users[userIndex].mail + '/calendarView');
}
q.drain = function(){
console.log("all tasks have completed");
}
我在几个if-else条件下动态地推回队列。如果需要,将包含此代码。