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
褋褋 @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.
await.its would seem to be a bug in https://github.com/nodejs/node/blob/master/lib/internal/repl/await.js
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! 馃ぉ
Most helpful comment
@devsnek happy to handle this!