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

[BUG] [1.4.0.1] Deferred import bindings break after await statements. #7662

Closed
trusktr opened this issue Aug 17, 2016 · 9 comments
Closed

[BUG] [1.4.0.1] Deferred import bindings break after await statements. #7662

trusktr opened this issue Aug 17, 2016 · 9 comments

Comments

@trusktr
Copy link
Contributor

@trusktr trusktr commented Aug 17, 2016

Found a funky one. Basically, the following code describes the issue, where the React variable becomes undefined after the await statement:

main()
async function main() {
    import React from 'react'
    import startup from 'awaitbox/meteor/startup'

    console.log(' --- React?', React) // defined. All is fine.
    await startup()
    console.log(' --- React?', React) // undefined! Uh oh!
}
@trusktr
Copy link
Contributor Author

@trusktr trusktr commented Aug 17, 2016

Here is a reproduction: https://github.com/trusktr/site/tree/meteor-issue-7662

To run:

git clone https://github.com/trusktr/site --branch meteor-issue-7662 --single-branch
cd site/meteor-app
npm install
meteor
@trusktr trusktr changed the title [BUG] [1.4.0.1] Await statements override deferred import bindings. [BUG] [1.4.0.1] Deferred import bindings break after await statements. Aug 17, 2016
@benjamn benjamn self-assigned this Aug 17, 2016
@benjamn
Copy link
Member

@benjamn benjamn commented Aug 17, 2016

This is a real bug! Thanks for bringing it to my attention.

The best way to solve this would be to turn reify into a Babel plugin so that it can run before babel-plugin-transform-regenerator, so Regenerator won't accidentally confine the imported variables to the switch statement that it generates.

A pretty good solution is just to run the reify compiler before any Babel plugins, so that's what I'll work on now.

@trusktr
Copy link
Contributor Author

@trusktr trusktr commented Aug 17, 2016

Wanted to note that for a workaround we can simply define the logic in a function before the await statement, then run the function after the await statement, so the above example becomes:

main()
async function main() {
    import React from 'react'
    import startup from 'awaitbox/meteor/startup'

    function logReact() {
        console.log(' --- React?', React) // defined. All is fine.
    }

    await startup()

    logReact() // it works
}
@benjamn
Copy link
Member

@benjamn benjamn commented Aug 17, 2016

Yep, there are lots of workarounds. The simplest is to move the import declarations out of the async function. Here's one version of that:

main()
function main() {
    import './silence-react'
    import React from 'react'
    import ReactDOM from 'react-dom'
    import startup from 'awaitbox/meteor/startup'

    return async function () {
        console.log(' --- React?', React) // defined. All is fine.
        await startup()
        console.log(' --- React?', React) // undefined! Uh oh!
    }();
}
@trusktr
Copy link
Contributor Author

@trusktr trusktr commented Aug 17, 2016

I really like deferred imports. They can solve circular dependency problems very neatly! What is the likelihood it making it into the ES spec? I'll bring it up in esdiscuss to help raise awareness.

@benjamn
Copy link
Member

@benjamn benjamn commented Aug 17, 2016

I'd hold off on bringing it up on esdiscuss, since I presented it at the last TC39 meeting, and I'm still working on a revised proposal with some other members.

@trusktr
Copy link
Contributor Author

@trusktr trusktr commented Aug 17, 2016

(I added repro steps to #7646. Are you able to reproduce?)

@adamgins
Copy link

@adamgins adamgins commented Nov 16, 2016

@benjamn I'm using await/sync to call Stripe... seems like I have a similar issue that await now returns "undefined" when I call Stripe.

Any hints on how to resolve this in the 1.4.2 stream pls?

benjamn added a commit that referenced this issue Dec 1, 2016
Run Reify before Babel to fix #8021 and #7662
@benjamn
Copy link
Member

@benjamn benjamn commented Dec 1, 2016

The import-in-async-functions bug should be fixed if you run meteor update --release 1.4.3-beta.0. Feel free to reopen this issue if that's not the case!

@benjamn benjamn closed this Dec 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.