testMultipleLoops2
在第一个之后的功能中for await
,
l
将转向GeneratorStatus:<closed>
,我进行了大量研究,但没有找到重新打开它的方法。
const tryRecursive=async function*(i=0){console.count("tryRecursive");yield i++;yield*tryRecursive(i)}
const asyncDelay=(b,delay=1e3)=>new Promise((resolve,reject)=>setTimeout(()=>resolve(b()),delay))
const tryDelayYieldNumbers=async function*(){
for await(const i of tryRecursive()){
const result=await asyncDelay(()=>i)
yield result}}
const testMultipleLoops2=(async()=>{
const l=tryDelayYieldNumbers()
let count=3
for await(const i of l){if(count-->0)console.log(i);else break}
count=4
///But `l` is closed here, can't do future looping
for await(const i of l){if(count-->0)console.log(i);else break}
count=5
for await(const i of l){if(count-->0)console.log(i);else break}
})()