2016-12-19 38 views
1

這是我的示例代碼。如何在循環中使用co庫?

response.results.forEach((val, index) => { 
    mu.co(function*() { 
     uIds[index].response = val 
     uIds[index].dateOnly = _.date 
     uIds[index].count = 1 
     yield _.insertUserIdWiseResponse(uIds[index]) 
    }) 
}) 

我想讓它同步。

+0

我要讓insertUserIdWiseResponse()這個函數同步 – rushUp

+0

insertUserIdWiseResponse() - 它包含了它日常合作 – rushUp

回答

0

你必須寫CO(mu.co(功能*(){)外循環語句,它應該工作。
但你必須用((的)環)

0

糾正使用辦法做到這一點是:

return mu.co(function*() { 
    for (let [index, val] of response.results.entries()) { 
     uIds[index].response = val 
     uIds[index].dateOnly = _.date 
     uIds[index].count = 1 
     yield notiResponse.upsert(uIds[index], notiResponse) 
    } 
})