好吧,在这里我可以在对象列表中进行一些验证,但函数验证会调用三个异步方法(向数据库发出请求)。但是当我在paralel中调用时,aplication会给出错误500。
_.each(titlesToSave, function(title, index) {
titleValidator.validateAsync(title, function (err) {
if (err) {
invalidTitles.push({index: index, error: err});
}
joinArray[index]();
});
});
var validateAsync = function (title, next) {
async.parallel([
function(callback){ checkCity(title.carrier.address, callback)},
function(callback){ checkCity(title.beneficiary.address, callback)},
function(callback){ checkCity(title.seller.address, callback)}
],
function (err,results) {
if(err){
console.log('ERROR: ' +err);
next(err);
}else{
console.log('Result: '+results);
next();
}
});
};
var checkCity = function(adress, callback){
var url = 'http://localhost:9000/api/city/'+adress.state+'/'+adress.city;
if (adress) {
http.get(url, function (res) {
console.log('STATUS: ' + res.statusCode);
res.on('data', function (chunk) {
if (res.statusCode === 200) {
console.log(''+chunk);
callback(null, chunk);
} else {
console.log('Invalid city: '+adress.city+', StatusCode: ' + res.statusCode);
callback(new Error('Invalid adress.'));
}
});
}).on('error', function (error) {
callback(error);
});
}
};
答案 0 :(得分:0)
试试这个,
async.eachSeries(titlesToSave, function (title, index, next) {
titleValidator.validateAsync(title, function (err) {
if (err) {
invalidTitles.push({index: index, error: err});
}
joinArray[index]();
next(null);
});
}, function (err) {
console.log(err);
});