Skip to content

for await transform incorrectly calls return when next throwsΒ #50525

@evanw

Description

@evanw

Bug Report

πŸ”Ž Search Terms

for await return next throw

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

let pass = true

async function f() {
  const y = {
    [Symbol.asyncIterator]() {
      let count = 0
      return {
        async next() {
          count++
          if (count === 2) throw 'error'
          return { value: count }
        },
        async return() {
          pass = false
        },
      }
    },
  }
  for await (let x of y) {
  }
}

f().catch(() => console.log({ pass }))

πŸ™ Actual behavior

The code prints { pass: false } when transformed by TypeScript with target ES2017 or lower.

πŸ™‚ Expected behavior

The code prints { pass: true } when run natively and when transformed by Babel. I discovered this behavior difference when investigating doing a similar transform for esbuild.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions