node.js async.series not working
Asked Answered
G

1

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?

Gigantopithecus answered 28/5, 2012 at 3:45 Comment(0)
S
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();
    }
]...
Saundrasaunter answered 28/5, 2012 at 4:9 Comment(6)
it works. But i wonder why in their wiki they mention it this way async.series([ function(){ ... }, function(){ ... } ]);Gigantopithecus
where is callback initialized here?Mump
This function appears to work even when I replace callback with callbach. Why does it work like this?Mump
@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'.Saundrasaunter
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: #12554517Mump
@JohnnyHK, can you have a look at an async related question here - #27646535Empress

© 2022 - 2024 — McMap. All rights reserved.