8

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?

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
Soyeed
  • 263
  • 1
  • 5
  • 12

1 Answers1

19

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();
    }
]...
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • 3
    it works. But i wonder why in their wiki they mention it this way async.series([ function(){ ... }, function(){ ... } ]); – Soyeed May 28 '12 at 12:18
  • where is `callback` initialized here? – Anderson Green Sep 22 '12 at 21:46
  • This function appears to work even when I replace `callback` with `callbach`. Why does it work like this? – Anderson Green Sep 22 '12 at 21:53
  • 1
    @AndersonGreen The `callback` parameter is provided by the `async` framework when it calls your methods. It's saying to you: 'call this callback method when your function has completed its work'. – JohnnyHK Sep 22 '12 at 22:31
  • Whenever I invoke (one function using async.series) inside (another function using async.series), it calls the functions in the wrong order, as detailed here: http://stackoverflow.com/questions/12554017/invoking-async-series-inside-async-series-produces-unpredictable-output – Anderson Green Sep 23 '12 at 16:27
  • @JohnnyHK, can you have a look at an `async` related question here - http://stackoverflow.com/questions/27646035/node-js-express-executing-mysql-queries-one-after-another-within-loops-in-a-s – Istiaque Ahmed Dec 26 '14 at 15:29