带有异步等待的节点repl [英] Node repl with async await
本文介绍了带有异步等待的节点repl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加对异步/等待节点repl的支持
I would like to add support to async/await to node repl
此问题之后: https://github.com/nodejs/node/issues/8382
我尝试使用此 https://github.com/paulserraino/babel-repl ,但是它缺少异步等待支持
I've tried to use this one https://github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文