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

"await is only valid in async function" when trying to use await in a for loop with TLA support #32708

Closed
serge1peshcoff opened this issue Apr 7, 2020 · 9 comments
Labels
confirmed-bug Issues with confirmed bugs. repl Issues and PRs related to the REPL subsystem.

Comments

@serge1peshcoff
Copy link

serge1peshcoff commented Apr 7, 2020

  • Version: 12.14.1
  • Platform: macOS 10.14.x
  • Subsystem:

What steps will reproduce the bug?

I run node with TLA support flag, I don't get the error if I do await on a top-level, but I do get the error if I do it in a for loop.

How often does it reproduce? Is there a required condition?

$ node --experimental-repl-await
Welcome to Node.js v12.8.1.
Type ".help" for more information.
> const timeout = delay => new Promise((res) => setTimeout(res, delay));
undefined
> timeout(1000).then(() => console.log('aaa'))
Promise { <pending> }
> aaa // this is displayed after 1 second, so it's fine
> await timeout(1000);
undefined // this is executed after 1 second, so it's also fine
> for (const i of [1, 2, 3]) {
... await timeout(1000);
Thrown:
await timeout(1000);
^^^^^

SyntaxError: await is only valid in async function
>

What is the expected behavior?

No error.

What do you see instead?

Error, see above.

Additional information

@addaleax addaleax added the repl Issues and PRs related to the REPL subsystem. label Apr 7, 2020
@hassaanp
Copy link
Contributor

hassaanp commented Apr 7, 2020

I think the issue is due to a premature error check that runs before the code block is allowed to be closed.
If you instead write:

for (const i of [1, 2, 3]) { await timeout(1000); }

It should work as expected.

@serge1peshcoff
Copy link
Author

@hassaanp yeah I've also just discovered it while trying to reproduce. Do you consider this a bug?

@serge1peshcoff
Copy link
Author

@hassaanp actually it can be that it's not only repl issue, this is the example that doesn't use repl and still fails (both with for...of loop written with 1 and 3 lines):

$ cat test.js
const timeout = delay => new Promise((res) => setTimeout(res, delay));

for (const i of [1, 2, 3]) { await timeout(1000); console.log(i); }
$ node --experimental-repl-await test.js
/Users/sergeypeshkov/oms-docker/oms-core-js/test.js:3
for (const i of [1, 2, 3]) { await timeout(1000); console.log(i); }
                             ^^^^^

SyntaxError: await is only valid in async function
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

@hassaanp
Copy link
Contributor

hassaanp commented Apr 7, 2020

I don't believe this is a bug.

From what I understand, repl behavior is configured by the --experimental-repl-await flag.
Using await in a node.js application is only allowed inside an async function. When you execute the file test.js, the --experimental-repl-await flag should not affect it since it runs like any standard node.js application.

I could be wrong. Maybe other contributors can chip in.

@serge1peshcoff
Copy link
Author

@hassaanp yeah but also node --experimental-repl-await test.js allows await statements on the top level (not within loops)

@devsnek
Copy link
Member

devsnek commented Apr 7, 2020

There is more general work to move towards a new method of evaluating repl input (using v8 inspector apis), which would fix a number of things, including handling TLA. because of that, i don't think anyone is specifically working on making our acorn parser hacks work better.

@serge1peshcoff
Copy link
Author

@devsnek is there an open issue on it? if so, I'd rather close this as part of it and subscribe to it instead

@devsnek
Copy link
Member

devsnek commented Apr 7, 2020

@serge1peshcoff v8:6903

@BridgeAR
Copy link
Member

This is working in newer Node.js versions. Closing.

@BridgeAR BridgeAR added the confirmed-bug Issues with confirmed bugs. label Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants