使用带有REPL的应用程序时如何解决承诺 [英] How to resolve promises when using app with REPL

查看:81
本文介绍了使用带有REPL的应用程序时如何解决承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的Node webserver(Koa.js +一个ORM)。我喜欢用 REPL 启动它,这意味着我可以像CLI工具一样使用我的应用程序。

I've got a basic Node webserver (Koa.js + a ORM). I like to start it with a REPL meaning I can use my app like a CLI-tool.

我所有的查询都返回Promises,但我不知道如何在REPL中解决它们。 如何解决这些问题?

All my queries return Promises but I don't know how I can resolve them in the REPL. How can I resolve them?

例如,以下代码(fetch()查询数据库并返回一个promise)仅提供此输出承诺{_bitField:4325376,_fulfillmentHandler0:undefined,_rejectionHandler0:undefined ...}

For example the following code (fetch() queries the database and returns a promise) gives only this output Promise {_bitField: 4325376, _fulfillmentHandler0: undefined, _rejectionHandler0: undefined …}

Transaction.where('reference', '1').fetch().then((res) => return res)


推荐答案

更新:Node.js现在默认执行此操作并解析承诺






旧答案:

Update: Node.js now does this by default and resolves promises


Old answer:

您无法正确解决它们 - 但您可以提取它们对全局范围的引用:

You can't resolve them proper - but you can extract their references to the global scope:

> Transaction.where('reference', '1').fetch().then((res) => out = res)
[Object Promise]
> out
  /* your data outputted here since the global was assigned to*/

我们可能允许在未来的节点中使用等待,这样可以更清晰地解决问题。

We might allow await in the REPL in the future in Node which would solve the issue more cleanly.

这篇关于使用带有REPL的应用程序时如何解决承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆