节点repl与asynchronous等待
我想添加对asynchronous/等待到节点repl的支持
在这个问题之后: https : //github.com/nodejs/node/issues/8382
我试图使用这一个https://github.com/paulserraino/babel-repl但它是缺lessasynchronous等待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上的自定义评估?
节点repl中的示例:
> const user = await User.findOne();
这个想法是预处理命令,并且如果在asynchronous函数之外有一个等待语法,则将其包装在一个asynchronous函数中
这个https://gist.github.com/princejwesley/a66d514d86ea174270210561c44b71ba是最终的解决scheme