我需要跳过async.series的函数或突破,并想知道我应该怎么做。我有一系列需要迭代的项目。我把该列表放在async.each函数中。然后,数组中的每个项目都会在继续之前通过一系列所需的函数(在下一个系列中需要信息)。但在某些情况下,我只需要通过第一个函数,然后如果不满足条件(例如,它是我们不使用的类别),则回调到下一个项目的async.each循环。以下是我的代码示例:
exports.process_items = function(req, res, next){
var user = res.locals.user;
var system = res.locals.system;
var likes = JSON.parse(res.locals.likes);
var thecat;
//process each item
async.each(items, function(item, callback){
//with each item, run a series of functions on it...
thecat = item.category;
async.series([
//Get the category based on the ID from the DB...
function(callback) {
//do stuff
callback();
},
//before running other functions, is it an approved category?
//if it is not an approved category, SKIP THE OTHER FUNCTIONS IN THE LIST (but how?)
function(callback) {
//do stuff
callback();
},
//some other functionality run on that item,
function(callback){
//do stuff
callback():
}
], function(err) {
if (err) return next(err);
console.log("done with series of functions, next item in the list please");
});
//for each like callback...
callback();
}, function(err){
//no errors
});
}
答案 0 :(得分:3)
将退出快捷方式放在相关函数的顶部。例如:
async.series([
//Get the category based on the ID from the DB...
function(callback) {
//do stuff
callback();
},
//before running other functions, is it an approved category?
//if it is not an approved category, SKIP THE OTHER FUNCTIONS IN THE LIST (but how?)
function(callback, results) {
if (results[0] is not an approved category) return callback();
//do stuff
callback();
},