4

我需要跳过 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
  });
}
4

1 回答 1

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();
    },
于 2013-07-22T22:25:39.287 回答