带有异步等待的节点repl

编程入门 行业动态 更新时间:2024-10-25 14:33:43
本文介绍了带有异步等待的节点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

本文发布于:2023-11-12 18:57:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   repl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!