45

I would like to add support to async/await to node repl

Following this issue: https://github.com/nodejs/node/issues/8382

I've tried to use this one https://github.com/paulserraino/babel-repl but it is missing async await suppport

I would like to use this snippet

const awaitMatcher = /^(?:\s*(?:(?:let|var|const)\s)?\s*([^=]+)=\s*|^\s*)(await\s[\s\S]*)/;
const asyncWrapper = (code, binder) => {
  let assign = binder ? `root.${binder} = ` : '';
  return `(function(){ async function _wrap() { return ${assign}${code} } return _wrap();})()`;
};

// match & transform
const match = input.match(awaitMatcher);
if(match) {
  input = `${asyncWrapper(match[2], match[1])}`;
}

How can I add this snippet to a custom eval on node repl?

Example in node repl:

> const user = await User.findOne();
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55

3 Answers3

88

As of node ^10, you can use the following flag when starting the repl:

node --experimental-repl-await
$ await myPromise()
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
  • 10
    Working great in node 10 and 12, and can be set in bashrc/zshrc with `export NODE_OPTIONS="--experimental-repl-await"` (so you always have it while debugging). – Elliot Nelson Jun 10 '20 at 12:32
  • 2
    Starting with node v16.6.0 (Jul 29, 2021. [changelog](https://github.com/nodejs/node/blob/v16.6.0/doc/changelogs/CHANGELOG_V16.md) & [commit a082a705b](https://github.com/nodejs/node/commit/a082a705b391712758ec9e296ca62653b458f841)), async-await in repl is enabled by default. You can opt out by passing [`--no-experimental-repl-await`](https://nodejs.org/docs/latest-v16.x/api/cli.html#--no-experimental-repl-await) . – li ki Feb 05 '22 at 17:02
8

There is the project https://github.com/ef4/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>

Another option, slightly onerous to start but with a great UI, is to use the Chrome Devtools:

$ node --inspect -r esm
Debugger listening on ws://127.0.0.1:9229/b4fb341e-da9d-4276-986a-46bb81bdd989
For help see https://nodejs.org/en/docs/inspector
> Debugger attached.

(I am using the esm package here to allow Node to parse import statements.)

Then you go to chrome://inspect in Chrome and you will be able to connect to the node instance. Chrome Devtools has top-level await, great tab-completion etc.

w00t
  • 17,944
  • 8
  • 54
  • 62
2

The idea is to preprocess the command and wrap it in a async function if there is an await syntax outside async function

this https://gist.github.com/princejwesley/a66d514d86ea174270210561c44b71ba is the final solution

Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55
  • Should we expect a promise or the result? The REPL does not hang until it gets the result you need to pause and resume it somehow, but afaik. that is not supported, at least `repl.pause()` exited by me. Another issue here: https://github.com/nodejs/node/issues/13209 – inf3rno Sep 14 '17 at 04:31