babel / babel/babel

Spec compliance: finally block of sync generator should not execute if used in async loop while yielding rejected promise

Open
#11,043 1 comment 0 reactions 0 assignees View on GitHub
i: needs triage
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

## Bug Report

**Current Behavior**
According to the spec the code
```js
function* generator() {
try {
yield Promise.resolve(2)
yield Promise.reject(new Error(3))
} finally {
print("finally")
}
}

// 1)
async function () {
for (const x of generator()) consume(await x)
}

// 2)
async function () {
for await (const x of generator()) consume(x)
}

// 3)
async function* () {
yield* generator()
}
```
should print "finally" only in the 1) case.

In the cases 2) and 3) finally block of the generator should not be executed.

Node@10 and above, current firefox and chromium have spec compliant behavior.

Babel when transpiling not node@8 has wrong behavior according to spec.

Now a lot of code is transpiled by people to the node@8 js version, and so async generators are transpiled by babel in the non-spec complained way. When the node@8 becomes obsolete and that code will be transpiled to node@10 then it can start to behave in a different way.

I suggest this should be fixed in the spec, not in the babel, though I think it's worth to make spec-compliance in some way.

Refs:
https://github.com/tc39/ecma262/issues/1849
https://bugzilla.mozilla.org/show_bug.cgi?id=1610315#c1

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the three generator examples in the issue and compare their behavior with Node, Firefox, Chromium, and Babel targeting Node 8. Read the linked tc39/ecma262 issue and Mozilla discussion to determine whether the required change belongs in the spec or Babel; done requires an agreed spec-compliant resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, javascript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.