Asynchronous async.parallel停止处理庞大的数组列表
我对异步模块还是新手。基本上,我希望使用async.parallel从阵列执行批更新操作,如下所示:Asynchronous async.parallel停止处理庞大的数组列表,asynchronous,mongoose,Asynchronous,Mongoose,我对异步模块还是新手。基本上,我希望使用async.parallel从阵列执行批更新操作,如下所示: var _ = require('lodash'); var async = require('async'); var list = req.body.dataArr; //the array var asyncTasks = []; _.each(hotelList,function(value,key){ asyncTasks.push(function(callback){
var _ = require('lodash');
var async = require('async');
var list = req.body.dataArr; //the array
var asyncTasks = [];
_.each(hotelList,function(value,key){
asyncTasks.push(function(callback){
Hotel.find({hotelId:value.hotelId}).exec(function(err,hotels){
if(err){
console.log(err);
}
if(hotels.length ==0){
//hotel not found
console.log('Hotel not found for ID :'+value.hotelId);
return;
}
hotels[0].countryCode = value.countryCode;
hotels[0].city = value.city;
hotels[0].save(function(err){
if(err){
console.log('saving failed ... Reason : '+err);
return;
}
console.log('saving successful');
});
});
});
});
async.parallelLimit(asyncTasks,function(){
//async.parallelLimit(asyncTasks,10, function(){
console.log('finish call asyncTask');
res.json({status:'success'});
});
saving successful
问题是,当我使用数组中的所有数据运行它时,有超过100.000个索引,它只会在没有任何消息的情况下停止,尽管我已经等待了几分钟,但当我尝试使用parallelLimit将数组限制为仅10个时,它只会执行10个更新操作,如下所示:
var _ = require('lodash');
var async = require('async');
var list = req.body.dataArr; //the array
var asyncTasks = [];
_.each(hotelList,function(value,key){
asyncTasks.push(function(callback){
Hotel.find({hotelId:value.hotelId}).exec(function(err,hotels){
if(err){
console.log(err);
}
if(hotels.length ==0){
//hotel not found
console.log('Hotel not found for ID :'+value.hotelId);
return;
}
hotels[0].countryCode = value.countryCode;
hotels[0].city = value.city;
hotels[0].save(function(err){
if(err){
console.log('saving failed ... Reason : '+err);
return;
}
console.log('saving successful');
});
});
});
});
async.parallelLimit(asyncTasks,function(){
//async.parallelLimit(asyncTasks,10, function(){
console.log('finish call asyncTask');
res.json({status:'success'});
});
saving successful
我使用异步的方式有什么问题吗?如果我的英语不好,很抱歉。您的asyncTasks函数需要在完成其工作后调用其回调参数,以便parallel或parallelLimit知道它们何时完成
_.eachhotelList,函数值,键{
asyncTasks.pushfunctioncallback{
Hotel.find{hotelId:value.hotelId}.execfunctioner,hotels{
伊弗{
console.logerr;
返回callbackerr;
}
ifhotels.length==0{
//找不到酒店
console.log'Hotel找不到ID:'+value.hotelId;
返回callbackError'notfound';
}
酒店[0]。countryCode=value.countryCode;
酒店[0]。城市=value.city;
酒店[0]。保存功能错误{
伊弗{
console.log“保存失败…原因:”+错误;
返回callbackerr;
}
console.log“保存成功”;
回调;
};
};
};
};