Skip to content

Normative: avoid mostly-redundant await in async yield*#2819

Merged
ljharb merged 1 commit intomainfrom
async-generator-yield-await-2
Dec 10, 2022
Merged

Normative: avoid mostly-redundant await in async yield*#2819
ljharb merged 1 commit intomainfrom
async-generator-yield-await-2

Conversation

@bakkot
Copy link
Copy Markdown
Member

@bakkot bakkot commented Jul 7, 2022

This is an alternative to #2818. See that PR for more context - this implements option 3 of the options listed there. Fixes #2813.

The main effect of this PR would be that if you had

let done = false;
let inner = {
  [Symbol.asyncIterator]: () => ({
    next() {
      if (done) {
        return Promise.resolve({ done: true });
      }
      done = true;
      return Promise.resolve({ done: false, value: Promise.resolve(0) });
    },
  }),
};

async function* outer() {
  yield* inner;
}

(async () => {
  for await (let x of outer()) {
    console.log(x);
  }
})().catch(e => {
  console.log('threw', e);
});

then you would see printed Promise.resolve(0) instead of, as currently, 0. In other words, it would behave as if you'd written for await (let x of inner) instead of of outer().

The other effect is a reduction in the number of promise ticks.

No difference (except in timing) would be observable without manually implementing Symbol.asyncIterator because async generators (and the async-from-sync wrapper, see step 6) already await values before yielding or returning them.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

has consensus This has committee consensus. has test262 tests normative change Affects behavior required to correctly evaluate some ECMAScript source text ready to merge Editors believe this PR needs no further reviews, and is ready to land.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

yield * in an async iterator should not call .throw when the inner iterator produces a rejected promise

5 participants