Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix _step.value access in for await #13491

Merged
merged 5 commits into from Jul 21, 2021

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jun 19, 2021

Q                       A
Fixed Issues? Fixes #13490
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

The relevant spec steps are 6.d-6.f of 14.7.5.7 ForIn/OfBodyEvaluation.

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Async Generators labels Jun 19, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Jun 19, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47012/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 19, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0927c7e:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Jun 19, 2021

It seems like the compiled code works in Node.js >= 12 but not in Node.js 10 🤔 Maybe it's because of tc39/ecma262#1250.

@nicolo-ribaudo
Copy link
Member Author

We should write a note in the docs that if someone cares about the exact number of ticks in await they need to make sure to enable transform-async-to-generator.

@JLHwung JLHwung self-requested a review June 20, 2021 00:52
@danieltroger
Copy link

We should write a note in the docs that if someone cares about the exact number of ticks in await they need to make sure to enable transform-async-to-generator.

Do you mean that this PR will be hidden behind some flag? If so I'm against it. I'd like my code to work without having to google for 30 minutes and figuring out I need to enable a flag. That's exactly what made me stop using tsc.

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Jun 21, 2021

No, this PR will be enabled by default. However, there are still some edge cases that are not handled.

Consider this code:

Promise.resolve()
  .then(() => console.log(1))
  .then(() => console.log(2))
  .then(() => console.log(3));

(async function () {
  console.log(await { then: f => f("A") });
  console.log("B");
})()

Both in Node.js 16 and in Node.js 8 it logs

1
2
A
B
3

However, if you consider this code:

Promise.resolve()
  .then(() => console.log(1))
  .then(() => console.log(2))
  .then(() => console.log(3));

(async function () {
  console.log(await Promise.resolve("A"));
  console.log("B");
})()

In Node.js 16 it logs

1
A
B
2
3

while in Node.js 8 it logs

1
2
3
A
B

The Node.js 8 behavior is not a Node.js 8 bug: Node.js 8 was correct, but then the spec was changed to produce the Node.js 16 output. A fix for this bug (which is similar to the one fixed in this PR) would require compiling async functions to generators in many browser that otherwise supports async functions:

  • Node.js 10 instead of Node.js 7.6
  • Chrome 68 instead of Chrome 55
  • Firefox 67 instead of Firefox 52

This breaking change in the ECMAScript specification affects a very small number of users (the majority of the web doesn't rely on the exact task queue ordering), but it can be easily worked around forcing Babel to compile async functions even when they are otherwise supported.

@nicolo-ribaudo
Copy link
Member Author

The CI failure is not related to this PR.

@existentialism existentialism changed the title Fix _step.value acces in for await Fix _step.value access in for await Jun 21, 2021
@danieltroger
Copy link

No, this PR will be enabled by default

Amazing, thanks a lot!

About the spec change: IMO the transpiled code should produce the same output in a modern browser as the untranspiled code in a modern browser, so transpiling it for Node 16 is the right thing to do I'd say.

@danieltroger
Copy link

@nicolo-ribaudo Sorry for being impatient, what is blocking this from being merged? I'd love to start using for await in my code.

@nicolo-ribaudo nicolo-ribaudo requested a review from hzoo June 24, 2021 15:26
@nicolo-ribaudo
Copy link
Member Author

Ping for reviews!

@danieltroger
Copy link

YEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEES. Thank you @hzoo ❤️

@nicolo-ribaudo nicolo-ribaudo merged commit 2b6acad into babel:main Jul 21, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the for-await-extra-tick branch July 21, 2021 17:12
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jul 30, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 21, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Async Generators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: transpiled for await has different behavior than untranspiled for await
5 participants