簡體   English   中英

如何在循環中使用co庫?

[英]How to use co library in loops?

這是我的示例代碼。

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

我想使其同步。

您必須在循環外編寫co(mu.co(function *(){)語句,它應該可以工作。
但是您必須將其與(for(of)循環一起使用)

正確的方法是:

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)
     }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM