有没有办法停止执行下一个与nodejsasynchronous系列function?

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){ }); }); 

是否有某种方式,如果函数1,如果booltest评估为真,不要继续下一个输出“2”的函数?

如果你callback真正的错误参数,基本上,stream程将停止

 if (booltest) callback(null, 'one'); else callback(true); 

应该pipe用

为了使其合乎逻辑,您可以将error重命名为errorOrStop类的errorOrStop

 var test = [1,2,3]; test.forEach( function(value) { async.series([ function(callback){ something1(i, callback) }, function(callback){ something2(i, callback) } ], function(errorOrStop) { if (errorOrStop) { if (errorOrStop instanceof Error) throw errorOrStop; else return; // stops async for this index of `test` } console.log("done!"); }); }); function something1(i, callback) { var stop = i<2; callback(stop); } function something2(i, callback) { var error = (i>2) ? new Error("poof") : null; callback(error); } 

我认为你正在寻找的function是async.detect不映射。

https://github.com/caolan/async#detect

检测(arr,迭代器,callback)

返回传递asynchronous真实性testing的arr中的第一个值。 迭代器是并行应用的,这意味着第一个返回true的迭代器将触发该结果的检测callback。 这意味着结果可能不是原来的ARR(按照顺序)通过testing的第一项。

示例代码

 async.detect(['file1','file2','file3'], fs.exists, function(result){ // result now equals the first file in the list that exists }); 

你可以用你的booltest来得到你想要的结果。

我通过一个对象来区分错误和正确的function。 看起来像:

 function logAppStatus(status, cb){ if(status == 'on'){ console.log('app is on'); cb(null, status); } else{ cb({'status' : 'functionality', 'message': 'app is turned off'}) // <-- object } } 

后来:

 async.waterfall([ getAppStatus, logAppStatus, checkStop ], function (error) { if (error) { if(error.status == 'error'){ // <-- if it's an actual error console.log(error.message); } else if(error.status == 'functionality'){ <-- if it's just functionality return } } });