Skip to content

Broken support for Top-Level Await #33479

Description

  • Version: v14.3.0
  • Platform: Windows 10 64-bit

What steps will reproduce the bug?

// test.js
await Promise.resolve(1);
$ node ./test.js

What is the expected behavior?

$ node ./test.js

What do you see instead?

$ node ./test.js
C:\workspace\test.js:2
await Promise.resolve(1);
^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:1116:16)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Activity

  1. richardlau commented on May 20, 2020

    @richardlau
    Member

    This feature is behind the --experimental-top-level-await flag. See https://github.com/nodejs/node/pull/33473.for updating the documentation.

  2. 8HoLoN commented on May 20, 2020

    @8HoLoN

    Even with node --experimental-top-level-await test-await.js, it is not working.

    => SyntaxError: await is only valid in async function

  3. BridgeAR commented on May 20, 2020

    @BridgeAR
    Member

    It will only work for esm modules. The default is to use the cjs loader. If you rename .js to .mjs it'll work properly. It will also work if you define the loader differently.

  4. 8HoLoN commented on May 20, 2020

    @8HoLoN

    It will only work for esm modules. The default is to use the cjs loader. If you rename .js to .mjs it'll work properly. It will also work if you define the loader differently.

    Could it work with the REPL ? (any flag to make REPL context a module context ?)

  5. BridgeAR commented on May 20, 2020

    @BridgeAR
    Member

    @8HoLoN we have experimental top level await in the REPL but it's not working identical to the "regular" top level await that's implemented in v14.3.0. You have to use the --experimental-repl-await flag to activate it.

  6. BridgeAR commented on May 20, 2020

    @BridgeAR
    Member

    Activating ESM is not yet supported in the REPL.

  7. changed the title [-]Brokren support for Top-Level Await[/-] [+]Broken support for Top-Level Await[/+] on May 20, 2020
  8. MylesBorins commented on May 20, 2020

    @MylesBorins
    Contributor

    I'm trying to figure out if there is something actionable here. Do we need to update docs or changelog? Should we close this?

  9. MylesBorins commented on May 20, 2020

    @MylesBorins
    Contributor

    We could also catch the TLA error in the CJS goal and give a better error message

  10. 8HoLoN commented on May 21, 2020

    @8HoLoN

    At minimum I would say, the mandatory flag should be mentioned in changelog and docs. (And that it is only working in top level of es module context)

  11. BridgeAR commented on May 23, 2020

    @BridgeAR
    Member

    This got fixed by #33473

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions