2017-08-28 37 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) { 
    // 
})