Search code examples
javascriptnode.jsasynchronousnode-async

Node.js async: How to combine multiple iterators with map?


I'm new to Node.js. I'm trying to do some asynchronous work using async where I have multiple nested callbacks, and I'd like to combine some of the functions into one if possible. Here is the code:

function getInfo(info, callback) { /*...*/ }
function pushInfos(infos, callback) { /*...*/ }

function upload(release, callback) {
    async.map(release.files, getInfo, function(err, infos) {
        if (err)
            return callback(err);
        pushInfos(infos, function(error) {
            if (error)
                return callback(error);
            // do some more work...
        });
    });
}

I'm wondering if there is a way to combine the getInfo and pushInfo functions so that I only need one nested callback. Something like this:

function upload(release, callback) {
    async.xxxx(release.files, [ // What goes here?
        getInfo,
        pushInfos
    ],
    function(error) {
        // do some more work...
    }
}

Does such an API exist or do I have to deal with the extra code? I tried looking at the documentation on GitHub but I don't have much experience in asynchronous programming, so it's a bit over my head.


Solution

  • Well, after a bit of playing around and reading the documentation, here is what my code looks like now:

    function upload(release, callback) {
        async.waterfall([
            async.apply(async.map, release.files, getInfo),
            pushInfos,
            function(next) {
                // do some work...
            }
        ], callback);
    }