Madhur Ahuja
Madhur Ahuja

Reputation: 22661

async/flow series on node.js

Consider this code:

var async = require('async');

var a = function()
{
    console.log("Hello ");
};
var b = function()
{
    console.log("World");
};


async.series(
[
    a,b
]
    );

Output is Hello

Why is World not part of the output?

Upvotes: 2

Views: 64

Answers (2)

Adam
Adam

Reputation: 4683

For each method called in series it is passed a callback method which must be run, which you are ignoring in your example.

The docs say:

tasks - An array or object containing functions to run, each function is passed a callback(err, result) it must call on completion with an error err (which can be null) and an optional result value.

The reason why your code is stopping after the first method is that the callback isn't being run, and series assumed an error occurred and stopped running.

To fix this, you have to rewrite each method along these lines:

var b = function(callback)
{
    console.log("World");
    callback(null, null) // error, errorValue
};

Upvotes: 2

nickclaw
nickclaw

Reputation: 698

The async.series function passes one callback to each of the methods that must be called before the next one is called. If you change your functions a and b to call the function it will work.

function a(done){
    console.log('hello');
    done(null, null); // err, value
}


function b(done){
    console.log('world');
    done(null, null); // err, value
}

Upvotes: 3

Related Questions