This uses the native async/await support in Node 7.x and newer to let you await
promises in the repl ("read-eval-print-loop" -- the interactive Node command line).
Note: Node 10 added the command line flag --experimental-repl-await
which allows await support. That may be a good option for new projects or usages.
npm install -g async-repl
$ async-repl
async> 1 + 2
3
async> 1 + await new Promise(r => setTimeout(() => r(2), 1000))
3
async> let x = 1 + await new Promise(r => setTimeout(() => r(2), 1000))
undefined
async> x
3
async>
const repl = require('repl');
const stubber = require('async-repl/stubber');
const replInstance = repl.start({ prompt: 'my-fancy-repl> ' });
stubber(replInstance);
-
This tool doesn't support multi-line input.
-
Top-level object destructuring assignment like:
let { x } = await someThing()
doesn't currently work due to a bug in
recast
(see test suite), but you can workaround like:({x} = await someThing())
-
We necessarily resolve the top-level expression promise for you, so if you don't want to wait for resolution you should make sure you're not returning the promise as an expression.
# This will not block because it's a statement let myPromise = makePromise() # This will block because it's an expression otherPromise = makePromise() # This will block because now we're using the promise value as an expression myPromise
-
const
variables in the repl scope can be overriddenasync> const a = 1 undefined async> a = 2 2