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