await 关键词


¥await keyword

在顶层启用对 await 关键字的支持。

¥Support for the await keyword is enabled at the top level.

> await Promise.resolve(123)
123
> await Promise.reject(new Error('REPL await'))
Uncaught Error: REPL await
    at REPL2:1:54
> const timeout = util.promisify(setTimeout);
undefined
> const old = Date.now(); await timeout(1000); console.log(Date.now() - old);
1002
undefined 

在 REPL 中使用 await 关键字的一个已知限制是,它将使 const 关键字的词法作用域无效。

¥One known limitation of using the await keyword in the REPL is that it will invalidate the lexical scoping of the const keywords.

例如:

¥For example:

> const m = await Promise.resolve(123)
undefined
> m
123
> 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 应禁用 REPL 中的顶层等待。

¥--no-experimental-repl-await shall disable top-level await in REPL.