Search code examples
javascriptnode.jsasynchronousnode-async

how to synchronize 2 async.waterfalls


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?


Solution

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

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