NodeJS Async - 继续执行多个 http 请求,即使某些请求失败
NodeJS Async - Continue execution for multiple http requests even if some fail
我正在尝试发出多个 HTTP 请求并累积,使用以下代码在 NodeJS 中显示结果:
const async = require('async');
const request = require('request');
function httpGet(url, callback) {
const options = {
url : url,
json : true
};
request(options,
function(err, res, body) {
console.log("invoked")
callback(err, body);
}
).on('error', function(err) {
console.log(err)
});
}
const urls= [
"http://1.2.3.4:30500/status/health/summary",
"http://5.6.7.8:30505/status/health/summary"
];
async.map(urls, httpGet, function (err, res){
if (err)
console.log(err);
else
console.log(res);
});
这里的问题是,如果第一个请求(http://1.2.3.4:30500/status/health/summary)失败(如连接被拒绝等),第二个请求不会通过。我知道我犯了一个愚蠢的错误,但找不到它。任何帮助表示赞赏!
如果你想async.map
不要快速失败,你可以这样做
const async = require('async');
const request = require('request');
function httpGet(url, callback) {
const options = {
url : url,
json : true
};
request(options,
function alwaysReportSuccess(err, res, body) {
callback(null, {
success: !err,
result: err ? err : body
});
}
).on('error', function(err) {
console.log(err)
});
}
const urls= [
"http://1.2.3.4:30500/status/health/summary",
"http://5.6.7.8:30505/status/health/summary"
];
async.map(urls, httpGet, function alwaysOk(_, res){
console.log(res); // will be an array with success flags and results
});
在 async.map 中,如果其中一个调用将错误传递给其回调,则将立即调用主回调(用于 map 函数)并显示错误(这就是您的情况的问题)。为了不在第一个错误时终止,请不要在您的 httpGet 中使用错误参数调用回调。
使用 async each,它接收一个参数列表和一个函数,并用每个元素调用函数,确保在你的 httpGet 里面在错误时调用回调,没有错误,这将使其余的即使在某些调用中出现错误,调用也会继续。这也适用于地图,但我认为更适合您的情况的功能是 async.each,而不是地图,您也可以使用 eachLimit 方法限制并发调用的数量。
勾选https://caolan.github.io/async/docs.html#each
const async = require('async');
const request = require('request');
function httpGet(url, callback) {
const options = {
url : url,
json : true
};
request(options,
function(err, res, body) {
if (err){
console.log(err);
callback();
return;
}
console.log("invoked")
callback(null, body);
}
).on('error', function(err) {
console.log(err);
callback();
});
}
const urls= [
"http://1.2.3.4:30500/status/health/summary",
"http://5.6.7.8:30505/status/health/summary"
];
async.each(urls, httpGet, function (err, res) {
}, function (err, res) {
});
我正在尝试发出多个 HTTP 请求并累积,使用以下代码在 NodeJS 中显示结果:
const async = require('async');
const request = require('request');
function httpGet(url, callback) {
const options = {
url : url,
json : true
};
request(options,
function(err, res, body) {
console.log("invoked")
callback(err, body);
}
).on('error', function(err) {
console.log(err)
});
}
const urls= [
"http://1.2.3.4:30500/status/health/summary",
"http://5.6.7.8:30505/status/health/summary"
];
async.map(urls, httpGet, function (err, res){
if (err)
console.log(err);
else
console.log(res);
});
这里的问题是,如果第一个请求(http://1.2.3.4:30500/status/health/summary)失败(如连接被拒绝等),第二个请求不会通过。我知道我犯了一个愚蠢的错误,但找不到它。任何帮助表示赞赏!
如果你想async.map
不要快速失败,你可以这样做
const async = require('async');
const request = require('request');
function httpGet(url, callback) {
const options = {
url : url,
json : true
};
request(options,
function alwaysReportSuccess(err, res, body) {
callback(null, {
success: !err,
result: err ? err : body
});
}
).on('error', function(err) {
console.log(err)
});
}
const urls= [
"http://1.2.3.4:30500/status/health/summary",
"http://5.6.7.8:30505/status/health/summary"
];
async.map(urls, httpGet, function alwaysOk(_, res){
console.log(res); // will be an array with success flags and results
});
在 async.map 中,如果其中一个调用将错误传递给其回调,则将立即调用主回调(用于 map 函数)并显示错误(这就是您的情况的问题)。为了不在第一个错误时终止,请不要在您的 httpGet 中使用错误参数调用回调。
使用 async each,它接收一个参数列表和一个函数,并用每个元素调用函数,确保在你的 httpGet 里面在错误时调用回调,没有错误,这将使其余的即使在某些调用中出现错误,调用也会继续。这也适用于地图,但我认为更适合您的情况的功能是 async.each,而不是地图,您也可以使用 eachLimit 方法限制并发调用的数量。
勾选https://caolan.github.io/async/docs.html#each
const async = require('async');
const request = require('request');
function httpGet(url, callback) {
const options = {
url : url,
json : true
};
request(options,
function(err, res, body) {
if (err){
console.log(err);
callback();
return;
}
console.log("invoked")
callback(null, body);
}
).on('error', function(err) {
console.log(err);
callback();
});
}
const urls= [
"http://1.2.3.4:30500/status/health/summary",
"http://5.6.7.8:30505/status/health/summary"
];
async.each(urls, httpGet, function (err, res) {
}, function (err, res) {
});