是否有可能在内部使用Generators代替async / await函数queue.add(...)?
代替此(它起作用):
queue.add(async () => {
await Api.getSomeInfo()
})
我需要使用类似这样的东西(不起作用):
queue.add(function* () {
yield Api.getSomeInfo()
})
根据您的需要,您可以编写帮助程序函数以将生成器转换为异步函数,诸如此类
const toAsync = (generator) => async () => {
let g = generator()
let result = g.next();
while (!result.done) {
const val = await result.value
console.log(val)
result = await g.next();
}
}
const delay = (arg) => new Promise(r => setTimeout(() => r(arg),1000))
queue.add(toAsync(function* myGenerator() {
for (let i = 0; i < 5; i++) {
yield delay(i)
}
}))