class Emitter {
constructor(max) {
this.max = max
this.asyncIdx = 0
}
async *[Symbol.asyncIterator]() {
while (this.asyncIdx < this.max) {
yield new Promise((resolve) => resolve(this.asyncIdx++))
}
}
}
async function asyncCount() {
let emitter = new Emitter(5)
console.log('emitter', emitter);
for await (const x of emitter) {
console.log(x);
}
}
asyncCount()
Symbol.asyncIterator
最新推荐文章于 2024-04-07 15:52:50 发布