doc: update example of using await in REPL

Clarify that the lexical scope of `const` is
invalidated when using top-level `await` in REPL.

As part of this change also add tests for the
documented behavior

Fixes: https://github.com/nodejs/node/issues/45918

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Co-authored-by: Dario Piotrowicz <dario.piotrowicz@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/57653
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Dario Piotrowicz 2025-04-05 23:06:45 +01:00 committed by GitHub
parent 214e3d4aff
commit 13e3aef053
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -258,8 +258,7 @@ undefined
```
One known limitation of using the `await` keyword in the REPL is that
it will invalidate the lexical scoping of the `const` and `let`
keywords.
it will invalidate the lexical scoping of the `const` keywords.
For example:
@ -268,10 +267,11 @@ For example:
undefined
> m
123
> const m = await Promise.resolve(234)
undefined
> m
> m = await Promise.resolve(234)
234
// redeclaring the constant does error
> const m = await Promise.resolve(345)
Uncaught SyntaxError: Identifier 'm' has already been declared
```
[`--no-experimental-repl-await`][] shall disable top-level await in REPL.

View File

@ -173,6 +173,12 @@ async function ordinaryTests() {
'3',
'undefined',
]],
// Testing documented behavior of `const`s (see: https://github.com/nodejs/node/issues/45918)
['const k = await Promise.resolve(123)'],
['k', '123'],
['k = await Promise.resolve(234)', '234'],
['k', '234'],
['const k = await Promise.resolve(345)', "Uncaught SyntaxError: Identifier 'k' has already been declared"],
// Regression test for https://github.com/nodejs/node/issues/43777.
['await Promise.resolve(123), Promise.resolve(456)', 'Promise {', { line: 0 }],
['await Promise.resolve(123), await Promise.resolve(456)', '456'],