2

我想支持添加到异步/等待到节点REPL与异步节点REPL伺机

关注这一问题:https://github.com/nodejs/node/issues/8382

我试图用这个https://github.com/paulserraino/babel-repl但它缺少异步等待suppport

我想用这个片段

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])}`; 
} 

我怎么能这个片段添加到节点REPL定制的eval?

实施例中节点REPL:

> const user = await User.findOne(); 
+0

如何在'--harmony'中使用普通的节点7 REPL? – estus

+0

node -harmony-async-await在节点7 REPL不起作用 –

+0

嗯,应该。我会建议检查节点版本。显然,'await'应该在'async'函数内。 – estus

回答

1

的想法是预处理命令,敷在一个异步函数如果 存在AWAIT语法外异步函数

https://gist.github.com/princejwesley/a66d514d86ea174270210561c44b71ba是最终解

+0

我们应该期待一个承诺还是结果? REPL不会挂起,直到它得到你需要暂停并恢复它的结果,但afaik。这是不支持的,至少'repl.pause()'由我退出。另一个问题在这里:https://github.com/nodejs/node/issues/13209 – inf3rno