我有以下测试代码示例:
function *someSequence () {
yield new Promise(resolve => setTimeout(resolve, 1000))
const result = yield testFunc()
yield console.log(result) // expecting "string", not undefined
}
function testFunc() {
const data = new Promise(resolve => setTimeout(() => resolve("string"), 5000))
data.then(() => data)
}
我需要在解析后立即将testFunc's
返回写入result
常量,然后在下一个产量中使用此结果。
我不知道序列中有多少个产量。
如果我将for of
语法与return
一起使用-生成器将在第一个收益率时退出。
for await (const item of someSequence) {
return item
}
我如何循环遍历所有收益(同步和异步)并取回它们返回的收益?
不确定为什么要写一个const,但是这是我要做的: