Now this code works just fine
async *[Symbol.asyncIterator](){
var promise;
while (true){
promise = this.#HEAD.promise;
this.size--;
this.#HEAD.next ? this.#HEAD = this.#HEAD.next
: this.#LAST = void 0;
yield await promise;
};
};
Say if i don't want to use the async / await
abstraction then how can i implement the same functionality only with promises?
I naively tried
*[Symbol.asyncIterator](){
var promise;
while (true){
promise = this.#HEAD.promise;
this.size--;
this.#HEAD.next ? this.#HEAD = this.#HEAD.next
: this.#LAST = void 0;
promise.then(yield);
};
};
but it returns undefined
; presumingly yield
not being a function. I checked out this question but it's not about generators and no yield
is involved. Is there a way to implement this?
Edit: yield await promise
in an async generator seems to be wasteful. Use yield promise
instead. Check the comments under T.J. Crowders answer.