Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use await in the NodeJs terminal?

I often start up the Node Js terminal to run small tasks or inspect some data. A current limitation is that I can't wait for an async function:

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

I end up having to do things like this

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

but there are some inconveniences associated with this. Is there any other alternative so that I can chain a sequence of await commands in the node terminal?

like image 768
Felipe Avatar asked Aug 29 '19 14:08

Felipe


1 Answers

Via this site:

Top Level Await is supported in Node version 10's REPL if you enable it with the --experimental-repl-await flag.

The official documentation is here and still requires the command line switch as of version 13.10.1.

like image 138
Quentin Avatar answered Sep 22 '22 08:09

Quentin