如何同步2 async.waterfalls

时间:2013-12-18 00:44:46

标签: javascript node.js asynchronous node-async

我有一组读命令,我必须按顺序。任何失败,处理停止。

readCommands是一个读取函数数组......

async.waterfall(readCommands, function(err) {
    if (err) {
        console.log(err);
        res.send(500, { error : err.toString() });
        return;
    }
    else {
        console.log('we determined the state to which we have to rollback to');
    }
});

此时,我知道我的开始。现在我想做写命令

async.waterfall(writeCommands, function(err) {
    if (err) {
        console.log(err);
        res.send(500, { error : err.toString() });
        return;
    }
    else {
        console.log('we succeeded with all the write commands...');
    }
});

数组readCommands和writeCommands条目完全不同,因此很难将它们组合起来。但是在我去下一个瀑布之前,我会为第一个瀑布完成。 如何从现有的两个“瀑布”中做出“瀑布”?

2 个答案:

答案 0 :(得分:2)

听起来很疯狂,但你实际上可以嵌套这些异步方法:

async.series([
  function (done) {
    async.waterfall(readCommands, done);
  },
  function (done) {
    async.waterfall(writeCommands, done);
  }
], function (err) {
  // All done
});

答案 1 :(得分:2)

只需组合两个数组,它们就会按顺序运行:

async.waterfall(readCommands.concat(writeCommands), function(err) {...}