0

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.

1 Answer 1

1

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

3 Comments

I just change the line with // pass parallel callback to trace the outcome comment. And it works perfectly. Just now there is a second argument in async.series, and it seems to be undocumented :(. Thx btw
@Kundu please use a 4 space indentation for code blocks instead of backticks.
@AlexisTyler Sorry for the inconvenience. I have used a mobile browser to add the answer. Thanks for the edit BTW.

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.