-1

I have a userBalance promise object with following values:

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

the problem is, i cannot get that 100000000000000000 from it. is there a way to do so?

alexmac
  • 19,087
  • 7
  • 58
  • 69
Tom Dawn
  • 185
  • 2
  • 3
  • 14

1 Answers1

3

The only way to get the resolution value of a promise (even if it's already resolved) is to consume the promise, via then or await:

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

or in an async function:

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

In both cases, plus error handling unless you're passing the resulting promise onto something else that will handle it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • how can i put that value into a if statement, like if (userBalance.then((value) => value == 0)){//do something} ? – Tom Dawn Jun 14 '18 at 09:03
  • @TomDawn - If you're using `then`, you don't; it's the other way around, the `if` goes in the `then` callback. Remember, the callback is asynchronous (see [this question's answers](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) for more). If you're using an `async` function and `await`, you'd just `if (await userBalance == 0)` since `async` functions let you express the logical flow rather than the synchronous flow. – T.J. Crowder Jun 14 '18 at 09:24