如何在NodeJs终端中使用await?

Fel*_*ipe 1 javascript terminal node.js

我经常启动Node Js终端以运行小任务或检查一些数据。当前的局限性是我不能等待异步功能:

mymachine$ node
> const request = require('request-promise-native')
undefined
> await request('https://google.com')
Thrown:
await request('https://google.com')
^^^^^

SyntaxError: await is only valid in async function
Run Code Online (Sandbox Code Playgroud)

我最终不得不做这样的事情

> let data;
undefined
> request('https://google.com').then(x => data = x)
Promise { <pending> }
> data.length
46262
Run Code Online (Sandbox Code Playgroud)

但这带来一些不便。还有其他选择可以使我await在节点终端中链接一系列命令吗?

Que*_*tin 7

通过此站点

如果您使用--experimental-repl-await标志启用节点10的REPL,支持顶级等待。