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

Top-level await does not seem to work in Meteor 3 #12986

Closed
alisnic opened this issue Jan 29, 2024 · 12 comments
Closed

Top-level await does not seem to work in Meteor 3 #12986

alisnic opened this issue Jan 29, 2024 · 12 comments
Labels
Meteor 3 relates to Meteor 3
Milestone

Comments

@alisnic
Copy link
Contributor

alisnic commented Jan 29, 2024

Given I have a file with the following contents:

import axios from "axios";

const getAndrei = async () => {
  const response = await axios.get('https://api.github.com/users/alisnic')
  return response.data
};

const Andrei = await getAndrei();

export default Andrei;

And then I import it:

import Andrei from './tla.js'

console.log('imported', Andrei)

Expected behaviour

json data from the response is logged

Actual behaviour

The output is imported { [Symbol(__esModule)]: true }.

In Meteor 2, everything works. Repo with repro - https://github.com/alisnic/meteor3-tla-repro (do a npm start to see the log).

meteor/packages

meteor-base@1.5.2-beta300.0             # Packages every Meteor app needs to have
mobile-experience@1.1.1-beta300.0       # Packages for a great mobile UX
mongo@2.0.0-beta300.0                   # The database Meteor supports right now
reactive-var@1.0.13-beta300.0            # Reactive variable for tracker

standard-minifier-css@1.9.3-beta300.0   # CSS minifier run for production mode
standard-minifier-js@3.0.0-beta300.0    # JS minifier run for production mode
es5-shim@4.8.1-beta300.0                # ECMAScript 5 compatibility for older browsers
ecmascript@0.16.8-beta300.0              # Enable ECMAScript2015+ syntax in app code
typescript@4.9.5-beta300.0              # Enable TypeScript syntax in .ts and .tsx modules
shell-server@0.6.0-beta300.0            # Server-side component of the `meteor shell` command
hot-module-replacement@0.5.4-beta300.0  # Update client in development without reloading the page
@StorytellerCZ StorytellerCZ added this to the Release 3.0 milestone Feb 5, 2024
@alisnic
Copy link
Contributor Author

alisnic commented Feb 21, 2024

@zodern maybe you can shed some light here? what am I doing wrong?

@jbo023
Copy link

jbo023 commented Feb 24, 2024

hmm, we have a similar problem, with Meteor 3 beta 4

collection.ts

export const Roles = await getRolesCollection();
model.ts 
import {
  Roles,
} from './collections';
console.log('rolesModel', Roles);

Roles here is undefined.

@perbergland
Copy link
Contributor

Any update here?

@perbergland
Copy link
Contributor

New simpler repro repo:

https://github.com/perbergland/tla-repro/tree/main

The produced reified code for tla-file.js does NOT look correct. It differs from the example in the reify readme file since it is not marked as async: true

/////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                         //
// server/tla-file.js                                                                      //
//                                                                                         //
/////////////////////////////////////////////////////////////////////////////////////////////
                                                                                           //
!module.wrapAsync(async function (module1, __reifyWaitForDeps__, __reify_async_result__) {
  "use strict";
  try {
    module1.export({
      tlaValue: () => tlaValue,
      someFunction: () => someFunction
    });
    let axios;
    module1.link("axios", {
      default(v) {
        axios = v;
      }
    }, 0);
    if (__reifyWaitForDeps__()) (await __reifyWaitForDeps__())();
    const asyncMethod = async () => {
      return 4;
    };
    console.log("before tla init");
    const tlaValue = await asyncMethod();
    console.log("after tla init");
    const someFunction = () => {
      return 7;
    };
    __reify_async_result__();
  } catch (_reifyError) {
    return __reify_async_result__(_reifyError);
  }
  __reify_async_result__()
}, {
  self: this,
  async: false
});
``

@perbergland
Copy link
Contributor

This is a bug in https://github.com/meteor/reify - it does not handle the case where the await is in the right hand side of a top level assignment, e.g.

const x = await functionCall();

I would rate this as a top level priority to fix for Meteor 3
https://github.com/meteor/reify does not have a separate issue list, so this is as good place as any to track the bug.

Workaround:
Add a top level await call in the imported file, e.g.

await 0;

anywhere in the file

@perbergland
Copy link
Contributor

@perbergland
Copy link
Contributor

Thanks to @zodern for the pointer to the reify code that needs fixing

@StorytellerCZ StorytellerCZ added the Meteor 3 relates to Meteor 3 label May 4, 2024
@leonardoventurini
Copy link
Contributor

I am taking a look at the fix here and checking if I can expand on that to cover a wider range of cases.

@leonardoventurini
Copy link
Contributor

This was fixed here, forgot to link the issue I think

meteor/reify#8

@Torgen
Copy link
Contributor

Torgen commented Aug 11, 2024

Is this believed to be fixed in 3.0.1? I'm still having this issue in a test that imports a top-level-await module, but at least the workaround of awaiting without assigning the result works for me.

@perbergland
Copy link
Contributor

I can confirm that this is NOT working in meteor 3.0.1 but I cannot figure out why. The workaround is still needed.
I checked the meteor version contents and the correct version of reify (0.25.2) is brought in.

Filing an issue in the meteor/reify repo

@denihs
Copy link
Contributor

denihs commented Oct 2, 2024

I've tested this with the PR #13298, and it worked as expected.

After the tests pass, we will merge it, and it will be included in version 3.0.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Meteor 3 relates to Meteor 3
Projects
None yet
Development

No branches or pull requests

7 participants