await 关键词


指定 --experimental-repl-await 命令行选项后,启用对 await 关键字的实验性支持。

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

在 REPL 中使用 await 关键字的一个已知限制是它会使 constlet 关键字的词法范围无效。

例如:

> const m = await Promise.resolve(123)
undefined
> m
123
> const m = await Promise.resolve(234)
undefined
> m
234

With the --experimental-repl-await command-line option specified, experimental support for the await keyword is enabled.

> await Promise.resolve(123)
123
> await Promise.reject(new Error('REPL await'))
Error: REPL await
    at repl:1:45
> const timeout = util.promisify(setTimeout);
undefined
> const old = Date.now(); await timeout(1000); console.log(Date.now() - old);
1002
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.

For example:

> const m = await Promise.resolve(123)
undefined
> m
123
> const m = await Promise.resolve(234)
undefined
> m
234