nodeJSdevise模式打破了asynchronousstream水或承诺。然后,然后

我有4个asynchronous函数在瀑布式我的节点应用程序中执行。

async.waterfall会把它整理一下,但是根据第一个函数的结果,我要么是错误的,要么进行瀑布或者是爆发到完成的function。

目前没有办法做到这一点(除了错误,成功的价值,只是闻起来不好)。

我正在看使用诺言(蓝鸟),但是,我再也看不到有什么办法可以突破,如果我没有在那个时候再给出另外一个诺言,那么下面的所有内容都会被执行。参数。

有其他的模式/工具可以帮助吗?

或者我在想这个,只是调用第一个方法,然后根据结果运行async.waterfall,或返回结果!

假设我有4个函数可以调用,a,b,c,d …..

 async.waterfall([ function(cb) { a("some data", cb) }, function(data, cb) { if(data) { // HERE we want to skip the remaining waterfall a go to the final func } else { b("some data", cb); } }, c, d ], function(err, data) { //do something }); 

或者如果他们是承诺…

 a("some data") .then(function(data) { if(data) { // here I want to stop executing the rest of the promises... } else { return b("some data") } }) .then(c) .then(d) .catch(function(err) { //log error }) .finally(function() { // do something }) 

我在想我应该这样做…

 function doit(data, done) { a("some data", function(err, data) { if(err) { return done(err) }; if(data) { return done(null, data) }; //Here we run the waterfall if the above doesn't exist. async.waterfall([ function(cb) { b("some data", cb) }, c, d ], function(err, data) { done(err, data); }); } 

通过承诺,您可以将“跟进承诺”附加到您需要的执行分支:

 a("some data") .then(function(data) { if(data) { return "foo";//other promises won't be executed here } else { return b("some data").then(c).then(d) } }) .catch(function(err) { //log error }) .finally(function() { // do something })