You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"await is only valid in async function" when trying to use await in a for loop with TLA support #32708
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
>
@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)
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.
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.
Activity
hassaanp commentedon 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:
It should work as expected.
serge1peshcoff commentedon Apr 7, 2020
@hassaanp yeah I've also just discovered it while trying to reproduce. Do you consider this a bug?
serge1peshcoff commentedon Apr 7, 2020
@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):
hassaanp commentedon Apr 7, 2020
I don't believe this is a bug.
From what I understand, repl behavior is configured by the
--experimental-repl-awaitflag.Using
awaitin a node.js application is only allowed inside an async function. When you execute the filetest.js, the--experimental-repl-awaitflag should not affect it since it runs like any standard node.js application.I could be wrong. Maybe other contributors can chip in.
serge1peshcoff commentedon Apr 7, 2020
@hassaanp yeah but also
node --experimental-repl-await test.jsallowsawaitstatements on the top level (not within loops)devsnek commentedon 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 commentedon Apr 7, 2020
@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 commentedon Apr 7, 2020
@serge1peshcoff v8:6903
BridgeAR commentedon Nov 29, 2021
This is working in newer Node.js versions. Closing.