Skip to content

Async Iteration Bug - yield* for await break #9021

@amirmohsen

Description

@amirmohsen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Has PRSpec: Async GeneratorsoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions