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

问题描述

我有一个对象,当我打印时它正在返回 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)
 })

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

09-06 00:09