Question

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");

        }
    ]);
Was it helpful?

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);
    },        
    ]);​

OTHER TIPS

You are supposed to provide async.series only with functions. The first items in the array are not functions. You need to wrap these calls in ones.

async.series([
  function () {
    collection.find().count(function () { … });
  },
  function () {
    collection.find().count(function () { … });
  },
  function () {
    console.log();
  },
  function () {
    console.log();
  }
]);

The first two items in the collection don't look like functions, it looks like you're invoking the first two functions immediately - or does count() return a function?

If you are invoking them, and not passing functions to async that's why it's choking before getting to the last two items.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top