Skip to content

async iterator does not call return() for an abrupt completion at for await of loopΒ #53106

@niceandneat

Description

@niceandneat

Bug Report

πŸ”Ž Search Terms

async iterator, for await loop

πŸ•— Version & Regression Information

  • This changed after version v4.9.3

⏯ Playground Link

Target: ES2017 (with actual result)
Playground link with relevant code

Target: ES2018 (with expected result)
Playground link with relevant code

πŸ’» Code

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2017",
  }
}

index.ts

const asyncIterable = {
  [Symbol.asyncIterator]() {
    return {
      next() {
        console.log('next()');
        return { value: '!', done: false };
      },
      return(value: string) {
        console.log('return()');
        return { value, done: true };
      },
    };
  }
};

const main = async () => {
  for await (const x of asyncIterable) {
    console.log(x);
    break;
  }
}

main();

Thanks to @kbridge for making MCVE.

πŸ™ Actual behavior

next()
!

Should call return() for an abrupt completion at for await of loop (e.g. break, throw). This happens when for-await-loop code is transformed to target equal or lower than ES2017.

πŸ™‚ Expected behavior

next()
!
return()

If target for compilation is equal or higher than ES2018, It works as expected. This is also aligned with javascript specification.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions