如何从javascript承诺中获得价值?
我有一个具有以下值的userBalance承诺对象:如何从javascript承诺中获得价值?,javascript,promise,Javascript,Promise,我有一个具有以下值的userBalance承诺对象: > userBalance Promise { '100000000000000000', domain: Domain { domain: null, _events: { removeListener: [Function: updateExceptionCapture], newListener: [Function: updateExceptionCapture], error: [
> userBalance
Promise {
'100000000000000000',
domain:
Domain {
domain: null,
_events:
{ removeListener: [Function: updateExceptionCapture],
newListener: [Function: updateExceptionCapture],
error: [Function: debugDomainError] },
_eventsCount: 3,
_maxListeners: undefined,
members: [] } }
问题是,我不能从中得到那一亿。有办法吗?获取承诺的解决值(即使已经解决)的唯一方法是使用承诺,通过
然后或等待:
userBalance.then(value => {
// ...use `value`; note this will be called asynchronously
});
或者在async
函数中:
const value = await userBalance; // Note this will be asynchronous
在这两种情况下,加上错误处理,除非您将结果承诺传递给将处理它的其他对象。我如何将该值放入if语句中,例如if(userBalance.then((value)=>value==0)){//do something}@TomDawn-如果您使用那么,则不需要;反过来说,如果
进入然后
回调。请记住,回调是异步的(有关更多信息,请参阅)。如果您使用的是async
函数和await
,则只需If(await userBalance==0)
,因为async
函数允许您表达逻辑流而不是同步流。