Node repl with asynchronous wait

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

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

I tried using this https://github.com/paulserraino/babel-repl but async wait suppport is missing

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 to add this snippet to custom eval on node repl?

An example in node repl:

> const user = await User.findOne();
+4
source share
2 answers

There is a 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>

, , , 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.

( esm , Node import.)

chrome://inspect Chrome, node. Chrome Devtools , ..

+3

Source: https://habr.com/ru/post/1665870/


All Articles