how to synchronize 2 async.waterfalls

617 Views Asked by At

I have a set of read commands that I have to in sequence. Any fails, processing stops.

readCommands is an array of read functions...

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

at this point, I know what I started with. Now I want to do the write commands

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...');
    }
});

the arrays readCommands and writeCommands entries are radically different so very hard to combine them. But I DO for the first waterfall to finish before I go to the next one. How do I make a "waterfall" out of the existing two?

2

There are 2 best solutions below

1
On BEST ANSWER

Just combine your two arrays, and they will run in sequence:

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

Sounds crazy, but you can actually nest these async methods:

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