Search code examples
javascriptnode-async

Not every function in Async.js series executes


I'm attempting to use Async.js to fire a series of asynchronous functions. Below is my code. Only the first two functions execute. The third and fourth functions in the series do not execute. I've simplified the functions thinking to the most basic possible. But still they do not execute. Can someone tell me what I've done wrong?

async.series([
        guessCollection.find( { user: user, imageFileName: imageFileName } ).count( function(err, number) {
        count = number;
        console.log(count);
        }),

        guessCollection.find( { user: user, imageFileName: imageFileName, correct: '1' } ).count( function(err, number) {
        correct = number;
        console.log(correct);
        }),

        function(){
            console.log("this text never doesn't get logged");
        },
        function() {
            console.log("neither does this text");

        }
    ]);

EDIT --- As suggested in the answers below, I made the first two proper functions. However now only the first function in the series executes. Functions 2-4 do not get called. I think there must be something else wrong in this code.

async.series([
        function(){
        guessCollection.find( { user: user, imageFileName: imageFileName } ).count( function(err, number) {
        count = number;
        console.log(count);
        })
    },
        function(){
        guessCollection.find( { user: user, imageFileName: imageFileName, correct: '1' } ).count( function(err, number) {
        correct = number;
        console.log(correct);
        })
    },

        function(){
            console.log("this text never doesn't get logged");

        },
        function() {
            console.log("neither does this text");

        }
    ]);

Solution

  • take a look to this code, it output only 1 2 3, because 3rd function not call the callback function, so series stops here. http://jsfiddle.net/oceog/9PgTS/

    ​async.series([
        function (c) {
            console.log(1);
            c(null);
        },        
        function (c) {
            console.log(2);
            c(null);
        },        
        function (c) {
            console.log(3);
    //        c(null);
        },        
        function (c) {
            console.log(4);
            c(null);
        },        
        ]);​