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

Async function hoisting is overly aggressive #6206

Closed
houli opened this issue Sep 6, 2017 · 3 comments
Closed

Async function hoisting is overly aggressive #6206

houli opened this issue Sep 6, 2017 · 3 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@houli
Copy link

houli commented Sep 6, 2017

Bug Report

The transformation of async function x to var x = function () { var _ref = _asyncToGenerator(... means that x must be hoisted. This is to avoid issues where x is referred to before it is defined. The problem is that the hoisting is overly aggressive and async functions appear before other statements in the code such as requires and imports. This is immediately noticeable if you try to write an async function in a module that sets up babel-polyfill/regenerator-runtime as a global using a side-effectful require/import. The result is broken code that fails as regeneratorRuntime will be undefined.

Input Code

Example using import
Example using require

Babel Configuration (.babelrc, package.json, cli command)

{
  "presets": ["env"]
}

Expected Behavior

The order of statements is preserved in a way that async functions are hoisted only as high as they need to be.

Current Behavior

Async functions are hoisted to the top of the module including to the point where they break the semantics of the import statement by appearing before it.

Your Environment

software version(s)
Babel 6.26.0
node 8.4.0
npm 5.3.0
Operating System macOS
@babel-bot
Copy link
Collaborator

Hey @houli! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@loganfsmyth
Copy link
Member

Currently this is the suggested approach: #4487 (comment)

We don't really have any way of knowing where to put the function. We could technically special-case babel-polyfill, but you could just as easily do import "./my-polyfills"; with babel-polyfill in another file and it would still break.

The best we could do is leave it as a function declaration and just rewrite it to remove the async instead, which would still break if you managed to call the function before the polyfill had executed. We may well do that at some point, but it hasn't happened yet because the recommendation above works for all cases and is easy to do.

cookpete added a commit to cookpete/auto-changelog that referenced this issue Oct 6, 2017
Fixes #8
Requires babel-polyfill to be imported before the main file due to aggressing async function hoisting
babel/babel#6206
@loganfsmyth
Copy link
Member

This should be fixed in 7.x

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 4, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants