2017-08-28 88 views
0

我试过aysnc.eachOfSeries,但代码没有循环。它会在第一次迭代中停止执行并冻结。我想我在回复回调时出错了。Async.eachOfSeries不循环

我也尝试在else块中放置回调,但给回调已经调用错误。

这个async.eachOfSeries嵌套在另一个eachOfSeries中。

async.eachOfSeries(data, function (value2, val, callback) { 
    let jsonObj = data[val]; 
    let email = jsonObj.ToEmail; 
    jsonObj['retailer'] = res[camp].retailer; 
    jsonObj['summary'] = 'f'; 
    let tempObj = {}; 
    tempObj[id] = jsonObj; 
    let options = { new: true}; 
    let campId = id; 
    User.addCampaignResponse(email, campId, tempObj, options, function (err, results) { 
     if (err) { 
       throw err; 
     } else { 
      console.log("gets printed once"); 
      Campaign.updateResponse(_id, function (err, results2) { 
       if (err) 
        throw err; 
       else { 
        console.log("Gets printed once"); 
        callback(); //tried this but gives callback already called error 
        } 
       }) // console.log(results); 
      } 
     }) 
    }, function (err) { 
     console.log("Doesn't print"); 
      callback(); 
    }) 

回答

0

它应该这样工作。

如果发生错误,这是发生在您的情况。它运行你提供的最终回调函数。

链接: - https://caolan.github.io/async/docs.html#eachOfSeries

当所有iteratee功能已经完成这就是所谓的回调, 或发生错误。用(err)调用。

2

您必须使用这样的:

//async.eachOfSeries(data, function (value2, val, callback) { => wrong 
async.eachOfSeries(data, function (value2, callback) { 
    // 
})