Search code examples
node.jsasynchronousnode-async

node.js async.series not working


This piece of code was taken straight out of the example from: https://github.com/caolan/async#seriestasks-callback

var async = require("async");
async.series([
    function() { console.log("a"); },
    function() { console.log("b"); }
], function(err, results){
    console.log(err);
    console.log(results);
});

However it doesn’t work. It stops after printing "a".

Is it a bug with the latest build of async module or my usage have some issue?


Solution

  • The functions you provide in the array passed into async.series need to accept a callback parameter that the function calls when the task is complete. So you'd want to do this instead:

    async.series([
        function(callback){ 
            console.log("a"); 
            callback();
        },
        function(callback){ 
            console.log("b");
            callback();
        }
    ]...