Node: `for await of` syntax doesn't work with --experimental-repl-await

Created on 23 Oct 2018  路  6Comments  路  Source: nodejs/node

  • Version: v10.9.0
  • Platform: Windows 10 Pro 10.0.17134 x64
  • Subsystem: REPL

The REPL is working great with regular await, but doesn't seem to be able to handle the new for await of syntax that was introduced as part of the async-iteration spec.

for await of works when wrapping the call with an async function, it only fails if used at the top level.

This fails:

$ node --experimental-repl-await
> for await (let i of [1,2,3]) console.log(i)
for await (let i of [1,2,3]) console.log(i)
    ^^^^^

SyntaxError: Unexpected reserved word

This works:

> (async () => { for await(let i of [1,2,3]) console.log(i) })()
Promise {
  <pending>,
  domain: ...etc }
> 1
2
3
promises repl

Most helpful comment

@devsnek happy to handle this!

All 6 comments

褋褋 @nodejs/repl

This is expected, await can be used only within async functions. Even from the link that you had shared, its says the following (below the code sample):

Async for-of statements are only allowed within async functions and async generator functions (see below for the latter).

That's the exact reason why it works perfectly fine, when you wrap with async.

Edit: Looks like repl supports top level await.

So the visitors don't check for a ForOfStatement with await: true, which means state.containsAwait ends up being false, and its not handled.

fix looks like this https://github.com/nodejs/repl/commit/2003bc5acb42f58285ce3b6de025006daced395d if anyone wants to take it

@devsnek happy to handle this!

Oh snap! Thank you so much for addressing this so quickly! 馃ぉ

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanjunzhi picture fanjunzhi  路  3Comments

dfahlander picture dfahlander  路  3Comments

danialkhansari picture danialkhansari  路  3Comments

jmichae3 picture jmichae3  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments