【发布时间】:2021-07-04 21:48:43
【问题描述】:
我有这个Queue 类(不是真正的实现,但它体现了我的观点):
class Queue {
constructor() {
this._arr = [];
}
async push(elem) {
this._arr.push(elem);
}
async pop() {
return this._arr.pop();
}
*[Symbol.asyncIterator]() {
do {
let res = await this.pop(); // here is the problem
if (res) yield res;
} while (res);
}
}
它只是一个 Javascript Array 的包装器,除了它的方法返回一个 Promise。
我想做的是根据pop() 方法的返回值有条件地产生,我不能这样做,因为await 不是asyncIterator 生成器函数内的有效操作。
我想过在上一次迭代中设置一个标志:
*[Symbol.asyncIterator]() {
let continue = true;
do {
yield this.pop().then(v => {
if (!v) continue = false;
return v
});
} while (continue);
}
但这仍然会在pop() 的最后一次执行中返回一个undefined 值。
我可以在调用代码中通过检查 undefined 值作为迭代结束的信号来处理这个问题,但我想知道是否有更好的方法来解决这个问题。
【问题讨论】:
-
对于真正的队列实现,可以看看this。它还恰当地实现了
[Symbol.asyncIterator](),没有任何生成器功能。
标签: javascript ecmascript-2018