Sib*_*ini 12 node.js async-await read-eval-print-loop ecmascript-2017
我想在节点repl中添加对async/await的支持
发布此问题:https://github.com/nodejs/node/issues/8382
我试过使用这个https://github.com/paulserraino/babel-repl但它缺少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])}`;
}
Run Code Online (Sandbox Code Playgroud)
如何将此代码段添加到节点repl上的自定义eval?
节点repl中的示例:
> const user = await User.findOne();
Run Code Online (Sandbox Code Playgroud)
Pau*_*erg 16
从节点^ 10开始,您可以在启动repl时使用以下标志:
node --experimental-repl-await
$ await myPromise()
Run Code Online (Sandbox Code Playgroud)
有项目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>
Run Code Online (Sandbox Code Playgroud)
另一个选项,稍微繁重但启动时有一个很好的用户界面,就是使用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.
Run Code Online (Sandbox Code Playgroud)
(我在这里使用esm包来允许Node解析import语句.)
然后,您转到chrome://inspectChrome,即可连接到该node实例.Chrome Devtools有顶级等待,很棒的标签完成等.
这个想法是预处理命令并将其包装在异步函数中(如果异步函数外部存在等待语法)
这个https://gist.github.com/princejwesley/a66d514d86ea174270210561c44b71ba是最终的解决方案
| 归档时间: |
|
| 查看次数: |
2554 次 |
| 最近记录: |