2

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?

0

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.