本文介绍了带有异步等待的节点repl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加对异步/等待节点repl的支持
I would like to add support to async/await to node repl
此问题之后: github/nodejs/node/issues/8382
我尝试使用此 github/paulserraino/babel-repl ,但是它缺少异步等待支持
I've tried to use this one github/paulserraino/babel-repl but it is missing async await 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上的自定义评估中?
How can I add this snippet to a custom eval on node repl?
节点repl中的示例:
Example in node repl:
> const user = await User.findOne(); 推荐答案截至节点^ 10 ,您可以在启动repl时使用以下标志:
As of node ^10, you can use the following flag when starting the repl:
node --experimental-repl-await $ await myPromise()更多推荐
带有异步等待的节点repl
发布评论