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 に答える