接收 Promise <Pending>而不是价值 [英] Receiving Promise <Pending> instead of value

查看:29
本文介绍了接收 Promise <Pending>而不是价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,当我打印时它返回 Promise <Pending>(我检查了 getRateable 的类型,它是对象)

I have a a object that when I'm printing that it's returning Promise <Pending> (I've checked the type of getRateable and it is object)

getRateable = getRateableEntitiesTx(tx, hashtagList);

我无法通过这个访问值:

I can't have access to the value by this :

getRateableEntitiesTx(tx, hashtagList).then((res) => {return res})

如果是 Promise 为什么它没有正确返回 res?

If it's a Promise why it's not returning the res properly?

提前感谢您的帮助

推荐答案

你不能从异步函数返回值,因为函数在接收到值之前就返回了.这就是为什么我们有承诺.您需要使用 then() 回调中的值:

You can't return the value from an async function because the function returns before the value has been received. That's why we have promises. You need to use the value from within the then() callback:

getRateableEntitiesTx(tx, hashtagList)
.then((rateable) => {
  // use rateable here
  console.log(rateable)
 })

这篇关于接收 Promise &lt;Pending&gt;而不是价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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