[英]How to use co library in loops?
This is my sample code. 这是我的示例代码。
response.results.forEach((val, index) => {
mu.co(function*() {
uIds[index].response = val
uIds[index].dateOnly = _.date
uIds[index].count = 1
yield _.insertUserIdWiseResponse(uIds[index])
})
})
I want to make it synchronized. 我想使其同步。
You have to write co ( mu.co(function*() {) statement outside the loop, it should work. 您必须在循环外编写co(mu.co(function *(){)语句,它应该可以工作。
But you have to use it with (for(of) loop) 但是您必须将其与(for(of)循环一起使用)
Correct way to do this is: 正确的方法是:
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.