已调用Async.ParallelLimit回调

时间:2016-01-28 12:13:22

标签: javascript node.js async.js asynchronous-javascript

我正在使用请求模块下载报告。我使用parallelLimit方法来限制同时执行的任务数。

迭代数组的代码是:

_.each(strs,function(str){
    _.each(experiences,function(selectedExperience){  
        asyncTasks.push(function(callback){
            downloadReport(name,inp.btime,inp.etime,selectedExperience,callback);
            });

        });
    });
});

异步任务功能的代码是:

 var downloadReport = function(Name,startTime,endTime,selectedExperience,callback){
    request.post({
                headers: myHeaders,
                uri: strurl,
                form: formObject,
                method: 'POST'
              }, function (err, res, body) {
              if(err){



        downloadReport(kpiName,startTime,endTime,selectedExperience,callback);            
                      }
                      else if(res)
                      {
                        if(body.indexOf("some string")>=0){
                              console.log("No records returned!!!");
                                callback(null,arr);                   
                          }
                          else if(!(body.indexOf("some string")>=0))
                          {     

                            var samp=body.substring(body.indexOf("\n")+1);

                            csv.fromString(samp, {headers: true})
                            .on("data",function(data){
                            arr.push(data);
                            })
                            .on("end",function(){ 
                                console.log("Array Length:",arr.length);
                                callback(null,arr);
                            });
                          }
                          else
                          {
                              //some code here
                                for (i=0;i<a.length;i=i+2){
                                    downloadReport(Name,a[i],a[i+1],selectedExperience,callback);//calling the same function
                                }
                          }
                      }
                  });

        }

异步并行限制的代码是

async.parallelLimit(asyncTasks,3,function(err, results){
    if(err) console.log(err);
    doSomething();
});

要求是在执行所有异步任务后仅调用一次doSomething()。但是我收到以下错误:

 if (fn === null) throw new Error("Callback was already called.");
               ^

在这种情况下如何只调用一次回调?

0 个答案:

没有答案