Skip to content

Commit 13e3aef

Browse files
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: #45918 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> Co-authored-by: Dario Piotrowicz <dario.piotrowicz@gmail.com> PR-URL: #57653 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 214e3d4 commit 13e3aef

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

doc/api/repl.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ undefined
258258
```
259259

260260
One known limitation of using the `await` keyword in the REPL is that
261-
it will invalidate the lexical scoping of the `const` and `let`
262-
keywords.
261+
it will invalidate the lexical scoping of the `const` keywords.
263262

264263
For example:
265264

@@ -268,10 +267,11 @@ For example:
268267
undefined
269268
> m
270269
123
271-
> const m = await Promise.resolve(234)
272-
undefined
273-
> m
270+
> m = await Promise.resolve(234)
274271
234
272+
// redeclaring the constant does error
273+
> const m = await Promise.resolve(345)
274+
Uncaught SyntaxError: Identifier 'm' has already been declared
275275
```
276276

277277
[`--no-experimental-repl-await`][] shall disable top-level await in REPL.

test/parallel/test-repl-top-level-await.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ async function ordinaryTests() {
173173
'3',
174174
'undefined',
175175
]],
176+
// Testing documented behavior of `const`s (see: https://github.com/nodejs/node/issues/45918)
177+
['const k = await Promise.resolve(123)'],
178+
['k', '123'],
179+
['k = await Promise.resolve(234)', '234'],
180+
['k', '234'],
181+
['const k = await Promise.resolve(345)', "Uncaught SyntaxError: Identifier 'k' has already been declared"],
176182
// Regression test for https://github.com/nodejs/node/issues/43777.
177183
['await Promise.resolve(123), Promise.resolve(456)', 'Promise {', { line: 0 }],
178184
['await Promise.resolve(123), await Promise.resolve(456)', '456'],

0 commit comments

Comments
 (0)