Skip to content

how to get final update from notifier via async iterable? #5924

Answered by kriskowal
dckc asked this question in Q&A
Discussion options

You must be logged in to vote

The aync iterator is working as expected. JavaScript’s iterator protocols support a final value but for loops do not surface it. There is a possibility that for expressions will eventually be possible in the language, such that the following would surface that final value:

const final = await for await (const medial of iterable) {}

But, today, to see the final value, you have to implement the iterator protocol by hand:

async function forAwaitEach(iterable, iterback)  {
  const iterator = iterator[Symbol.asyncIterator]();
  try {
    for (;;) {
      const {done, value} = await iterator.next();
      if (done) {
        return value; // <- this is the final value that for loops don’t surface

Replies: 3 comments

Comment options

dckc
Aug 10, 2022
Collaborator Author

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by dckc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants