如何从 JavaScript Promise 中获取价值?

2024-01-06

我有一个 userBalance 承诺对象,具有以下值:

> userBalance
Promise {
'100000000000000000',
domain:
 Domain {
   domain: null,
   _events:
    { removeListener: [Function: updateExceptionCapture],
      newListener: [Function: updateExceptionCapture],
      error: [Function: debugDomainError] },
   _eventsCount: 3,
   _maxListeners: undefined,
   members: [] } }

问题是,我无法从中得到 100000000000000000 。有办法这样做吗?


The only获取 Promise 的解析值(即使它已经解决)的方法是使用 Promise,通过then or await:

userBalance.then(value => {
    // ...use `value`; note this will be called asynchronously
});

或在一个async功能:

const value = await userBalance; // Note this will be asynchronous

在这两种情况下,加上错误处理,除非您将结果承诺传递给其他可以处理它的东西。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 JavaScript Promise 中获取价值? 的相关文章

随机推荐