async.each 是由 Async Lib 提供的非常有用且强大的功能。它有 3 个字段
1个集合/数组
2-迭代
3-回调
集合是指对象的数组或集合,迭代是指每次迭代,回调是可选的。
如果我们提供回调,那么它将返回您想在前端显示的响应或说出结果
将函数 iteratee 并行应用于 coll 中的每个项目。使用列表中的项目调用 iteratee,并在完成时回调。如果迭代者将错误传递给它的回调,主回调(对于每个函数)会立即调用错误。
请注意,由于此函数将 iteratee 并行应用于每个项目,因此无法保证 iteratee 函数将按顺序完成。
例子-
var updateEventCredit = function ( userId, amount ,callback) {
async.each(userId, function(id, next) {
var incentiveData = new domain.incentive({
user_id:userId,
userName: id.userName,
amount: id.totalJeeneePrice,
description: id.description,
schemeType:id.schemeType
});
incentiveData.save(function (err, result) {
if (err) {
next(err);
} else {
domain.Events.findOneAndUpdate({
user_id: id.ids
}, {
$inc: {
eventsCredit: id.totalJeeneePrice
}
},{new:true}, function (err, result) {
if (err) {
Logger.info("Update status", err)
next(err);
} else {
Logger.info("Update status", result)
sendContributionNotification(id.ids,id.totalJeeneePrice);
next(null,null);
}
});
}
});