goFrendiAsgard
goFrendiAsgard

Reputation: 4084

node-async doesn't execute callback

The Code

I tried this package https://www.npmjs.com/package/async to implement series & parallel processing.

For small cases it works, well, however when I try to run this, I get an unexpected behavior:

async.series([
    function(callback){
        console.log('one');
        callback();
    },
    function(callback){
        console.log('two');
        async.parallel([
            function(callback){
                console.log('two-one');
                callback();
            },
            function(callback){
                async.series([
                    function (callback){
                        console.log('two-two-one');
                        callback();
                    },
                    function (callback){
                        console.log('two-two-two');
                        callback();
                    }
                ]);
            }
        ], callback);
    },
    function(callback){
        console.log('three');
        callback();
    }
]);

The Expected Result

The code should print one, two, two-one, and three in series. However, after print two-one, I want to print two-two-one and two-two-two in parallel.

The expected result is either:

one
two
two-one
two-two-one
two-two-two
three

or

one
two
two-one
two-two-two
two-two-one
three

The Real Result

Unfortunately, three is never printed.

one
two
two-one
two-two-one
two-two-two

The Question

Is there something wrong with my code/understanding?

Thanks.

Upvotes: 0

Views: 59

Answers (1)

Kundu
Kundu

Reputation: 4054

Async series take a last argument as function to grab the final result.

Please pass parallel callback as second argument of async series.

async.series([
    function(callback){
        console.log('one');
        callback();
    },
    function(callback){
        console.log('two');
        async.parallel([
            function(callback){
                console.log('two-one');
                callback();
            },
            function(callback){
                async.series([
                    function (callback){
                        console.log('two-two-one');
                        callback();
                    },
                    function (callback){
                        console.log('two-two-two');
                        callback();
                    }
                ],callback);// pass parallel callback to trace the outcome 
            }
        ], callback);
    },
    function(callback){
        console.log('three');
        callback();
    }
],console.log);// use console.lg to get the final outcome of series

Upvotes: 1

Related Questions