繁体   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