Asyncjs : Bypass a function in a waterfall chain

左心房为你撑大大i 提交于 2019-12-04 04:51:28

An alternative might be:

var tasks = [f1];

if(myBool){
    tasks.push(f2);
}

tasks.push(f3);

async.waterfall(tasks, function(err, result){
});

where f1, f2, and f3 are your functions.

other than that, you're better off doing it explicitly, avoid making your code overly complicated, simpler is usually better

update:

function f1(done){
    if(myBool){
        f2(done);
    }else{
        done();
    }
}

function f2(done){
    async.nextTick(function(){
        // stuff
        done();
    });
}

async.waterfall([f1,f3],function(err,result){
    // foo
});

I think this should work:

var finalCallback = function(err, result){
  if(err)
     // handle error..
  else
     console.log('end! :D');
}

async.waterfall(
  [
    function step1(callback){
       // stuff
       callback(null, someData);
    },
    function step2(someData, callback){
       if(skip_step_3)
          finalCallback(null, someData);
       else
          callback(null, someData);
    },
    function step3(moreData, callback){
       // more stuff
       callback(null, moreData);
    }
  ],
  finalCallback
)

the creator of async recomend this in the github repo (https://github.com/caolan/async/pull/85)

Using if-async module your code will look like this:

var async = require('async')
var ifAsync = require('if-async')

async.waterfall([
    foo,
    ifAsync(p1).then(c1).else(c2),
    bar
], function(err) {})

for full example take a look here: https://github.com/kessler/if-async#example-2-using-with-asyncjs-waterfall

I am late to answer, but async-if-else might help you.

Sample Code

 var async = require('async-if-else')(require('async'));

  function emailExists(user, callback) {
    user.find(user.email, function(err, dbUser){
      if (err)
        return callback(error);

      if(!dbUser)
     return callback(null, false); // does not exist, predicate will be false 

   callback(null, true);  
  });
  }

  function updateAccount(user, callback) { 
    user.update( ..., callback);
  }

  function importFromLegacyByEmail(user, callback) { 
    remoteClient.get(user, callback);
  }

  async.waterfall([
    async.constant({email: 'thiago@email.com', dogs: 2, money: 0, fun: 100 }),
    async.if(emailExists, updateAccount).else(importFromLegacyByEmail),
    sendEmail
  ], handler);

I would recommend using clojurescript which has an awesome core-async library which makes life super easy when dealing with async calls.

In your case, you would write something like this:

(go
  (when-let [res1 (<! (asyncFunc1))]
    (<! (asyncFunc2 res1)))
  (<! (asyncFunc3)))

Note the go macro which will cause the body to run asynchronously, and the <! function which will block until the async functions will return.

The code will first block on the first async function. Then, if the result of that is true, it will run the second async function on block on that as well. Lastly, it will run the third async function and block on that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!