代码之家  ›  专栏  ›  技术社区  ›  Nafis

接受承诺<待定>而不是价值

  •  1
  • Nafis  · 技术社区  · 7 年前

    我有一个物体,当我打印时,它会返回 Promise <Pending> (我检查过 getRateable 它是目标)

    getRateable = getRateableEntitiesTx(tx, hashtagList);
    

    我无法通过以下方式访问该值:

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

    如果是一个 Promise 为什么它不返回 res 合适吗?

    提前谢谢你的帮助

    1 回复  |  直到 7 年前
        1
  •  0
  •   Mark    7 年前

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

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