2

How can I return value from promise. I want compare value of promise with my limit balance. But I can't return my result in my condition.

if (getBalanceByAddress(....) > 228) {
    console.log('ALL ok')
} else {
    console.log('insufficient funds')
}

getBalanceByAddress(addressFrom) {
    var _this = this;
    return _this.web3.eth.getBalance(addressFrom).then(function(result) {
        return result;
    });
}
Esko
  • 4,109
  • 2
  • 22
  • 37
  • Hint: when `getBalanceByAddress` returns, the call is still in progress, so the result is not available yet. You have to append result handling at the end of the promise chain. – spectras Dec 27 '17 at 11:35
  • How about trying a different approach :) getBalanceByAddress(....).then((result)=>{ if ( result > 228) { console.log('ALL ok') } else { console.log('insufficient funds') } }) getBalanceByAddress(addressFrom) { var _this = this; return _this.web3.eth.getBalance(addressFrom) } – orangespark Dec 27 '17 at 11:38

1 Answers1

3

You need to await it:

 (async function(){ 
  if( await getBalanceByAddress() > 228){
   console.log('ALL ok');
  } else {
   console.log('insufficient funds');
  }
 })()
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 2
    Even though attached duplicate link has answered it, I still liked this simple solution (no scrolling) +1. You still need to let OP know the browser support of this solution though! – gurvinder372 Dec 27 '17 at 11:42