简体   繁体   English

在 asyncIterator 生成器 function 中等待异步操作

[英]Wait for an async operation inside asyncIterator generator function

I have this Queue class (not the real implementation, but it exemplifies my point):我有这个Queue class (不是真正的实现,但它体现了我的观点):

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);
    }
}

It's just a wrapper for a Javascript Array with the exception that its methods return a Promise .它只是 Javascript Array的包装器,但其方法返回Promise

What I want to do is to yield conditionally based on the return value of the pop() method, which I cannot do because await is not a valid operation inside an asyncIterator generator function.我想做的是根据pop()方法的返回值有条件地产生,我不能这样做,因为await不是asyncIterator生成器 function 中的有效操作。

I thought about having a flag which is set in the previous iteration:我考虑过在上一次迭代中设置一个标志:

*[Symbol.asyncIterator]() {
    let continue = true;
    do {
        yield this.pop().then(v => {
            if (!v) continue = false;
            return v
        });
    } while (continue);
}

But this would still return a undefined value in the last execution of pop() .但这仍然会在pop()的最后一次执行中返回一个undefined的值。

I could handle this in the calling code by checking for a undefined value as a signal of the end of the iteration, but I was wondering if there was a better approach at tackling this problem.我可以在调用代码中通过检查undefined的值作为迭代结束的信号来处理这个问题,但我想知道是否有更好的方法来解决这个问题。

You can use an async generator function (MDN docs missing, but see eg this article ) for the implementation of the [Symbol.asyncIterator]() method :您可以使用async生成器 function (缺少 MDN 文档,但请参阅例如 这篇文章)来实现[Symbol.asyncIterator]()方法

async *[Symbol.asyncIterator]() { /*
^^^^^ */
    while (this._arr.length) {
        yield await this.pop(); // no longer a problem
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建异步迭代器的最佳实践是什么? 我应该使用异步生成器 function 还是使用 Symbol.asyncIterator? - What is the best practice to create an async iterator? Should I use an async generator function or rather use Symbol.asyncIterator? 等待异步操作的结果 - Wait for result of async operation 制作一个没有 async/await 但只有 Promise 的异步生成器 *[Symbol.asyncIterator] - Making an asynchronous generator *[Symbol.asyncIterator] without async/await but with promises only 带有for of循环和内部等待的异步function,它会等待吗? - Async function with for of loop and await inside, will it wait? 如何使代码在以下异步/等待功能中等待? - how to make the code wait inside the following async/await function? React onSubmit 等待 function 内的异步 IIFE 更新 state 与钩子 - React onSubmit wait for async IIFE inside the function to update state with hooks 在 Vuejs 中使用 async-await 等待函数内的赋值 - using async-await to wait for an assignment inside a function in Vuejs 如何在Javascript中的array.forEach中等待异步函数 - How to Wait on Async function inside array.forEach in Javascript 等待 map 内的所有异步 function 完成执行 - Wait all async function inside map to finish execution 如何等待异步功能? - How to wait for an async function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM