user1665355
user1665355

Reputation: 3393

Async.whilst inside async.series

I am using async.waterfall inside async.whilst, inside async.series like this (which only crashes once, but then continues as it should):

function(callback){

 async.whilst(
  function() { 
   console.log("Number of things left: " + classes.length); return classes.length > 0; 
  }, 
  function(innerCallback){
    //Define some variables.

    async.waterfall([
     function (next) {
      MyTable.find({ Data: theData},{ DataThis:1, Stuff:1, _id:0 },next).limit(1);
     },
     function (results, next) {
      if(results.length > 0 && results[0].theData=== parseInt(theInfo)){
       //Update my database
      } else{
       next();
      } 
     }
    ], setTimeout(function() { 
       //Splice the data. I.e. mydata.splice(i-1,1);                                            
       innerCallback(); 
    }, 0)); //closing async.waterfall.                      
  },
  function(err){
    // The final function of whilst 
    callback(err);
 }) // Whilst finished
}

But I get an error:

serverClasses2-3 TypeError: undefined is not a function
serverClasses2-3     at /home/node_modules/async/lib/async.js:52:16
serverClasses2-3     at /home/node_modules/async/lib/async.js:1204:30
serverClasses2-3     at /home/routes/indexClasses2.js:267:11
serverClasses2-3     at Query.callback (/home/node_modules/mongoose/lib/query.js:2021:7)
serverClasses2-3     at /home/node_modules/mongoose/node_modules/kareem/index.js:177:19
serverClasses2-3     at /home/node_modules/mongoose/node_modules/kareem/index.js:109:16
serverClasses2-3     at process._tickDomainCallback (node.js:381:11)

Have anyone encountered the same error?

Upvotes: 3

Views: 500

Answers (1)

Bergi
Bergi

Reputation: 664196

You're passing the result of setTimeout(…, 0) as the callback to async.waterfall. It shouldn't be undefined, but rather a timer object, but still it's not callable and will throw such an error.

Instead, just pass the callback directly:

async.waterfall([…], innerCallback)

Or use a function expression around the timeout if you need to do more.

Upvotes: 2

Related Questions