Async.whilst在async.series中

时间:2016-01-30 16:34:12

标签: javascript node.js asynchronous

我在async.waterfall内使用async.whilst,在async.series内{这只会崩溃一次,但接着应该继续):

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
}

但是我收到了一个错误:

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)

有没有人遇到过同样的错误?

1 个答案:

答案 0 :(得分:2)

您将setTimeout(…, 0)的结果作为回调传递给async.waterfall。它不应该是undefined,而是一个计时器对象,但它仍然不可调用并且会抛出这样的错误。

相反,只需直接传递回调:

async.waterfall([…], innerCallback)

如果您需要执行更多操作,请在超时周围使用函数表达式。