New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Iteration Bug - yield* for await break #9021

Open
amirmohsen opened this Issue Nov 14, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@amirmohsen

amirmohsen commented Nov 14, 2018

Bug Report

Current Behavior
When for await is used on a generator that uses yield* to delegate to another generator and we attempt to break out of the loop, the iteration stops but the code after yield* still runs. This is wrong and the native implementation in V8 doesn't have this problem.

This essentially means that with the code below, we get done:

0
'done'

Input Code
Link to example repo

async function* inner() {
  const ids = [0, 1, 2, 3];
  for await (const id of ids) {
    yield id;
  }
}

async function* wrapper() {
  yield* inner();
  console.log('done');
}

const run = async() => {
  for await (const id of wrapper()) {
    console.log(id);
    break;
  }
};

run();

Expected behavior/code
If you run the above code in the Chrome console, you get:

0

This is the desired behavior (as implemented natively in V8). Since we are breaking out of the loop, the execution inside the generator must not continue.

Babel Configuration (.babelrc, package.json, cli command)
Please check the repo link above for more details.

babel.config.js

module.exports = (api) => {
  api.cache(true);
  return {
    presets: [
      [
        '@babel/preset-env',
        {
          targets: {
            node: '8.11.3'
          }
        }
      ]
    ]
  }
}

package.json (partial)

{
  "scripts": {
    "build": "babel ./src -d ./dist -s",
    "start:dist": "node ./dist",
    "start": "babel-node ./src"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.5",
    "@babel/core": "^7.1.6",
    "@babel/node": "^7.0.0",
    "@babel/polyfill": "^7.0.0",
    "@babel/preset-env": "^7.1.6"
  }
}

Environment

  • Babel version(s): v7.1.6
  • Node/npm version: Node 8.11.3/npm 6.4.1
  • OS: OSX 10.13.6
  • Monorepo: no
  • How you are using Babel: cli
@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Nov 14, 2018

Hey @amirmohsen! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

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