有没有办法在nodejs中停止执行async系列的下一个函数?

Rol*_*ndo 11 node.js async.js

  async.map(list, function(object, callback) {
    async.series([
      function(callback) {
        console.log("1");

        var booltest = false;
        // assuming some logic is performed that may or may not change booltest
        if(booltest) {
            // finish this current function, move on to next function in series
        } else {
           // stop here and just die, dont move on to the next function in the series
        }

        callback(null, 'one');
      },
      function(callback) {
        console.log("2");
        callback(null, 'two');
      }
    ],
    function(err, done){
    });
  });
Run Code Online (Sandbox Code Playgroud)

有没有某种方法,如果函数1如果booltest评估为真,不要继续下一个输出"2"的函数?

dri*_*hev 22

如果你使用true作为错误参数回调,流程将停止,所以基本上

if (booltest)
     callback(null, 'one');
else
     callback(true);
Run Code Online (Sandbox Code Playgroud)

应该管用

  • 从文档中:*如果系列中的任何函数将错误传递给其回调,则不再运行任何函数,并立即使用错误值调用该系列的回调.*https://github.com/caolan/异步#系列 (5认同)
  • 我不认为这是正确的设计.第一个参数意味着错误.如果调用回调,异步就会停止处理,但是使用它作为一种通常的方式来停止任意原因的处理对我来说似乎很奇怪. (2认同)