2017-12-27 151 views
2

如何从承诺返回价值。我想比较承诺的价值和我的极限平衡。但是我无法将我的结果归因于自己的状况。如何从承诺返回价值

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; 
    }); 
} 
+0

提示:当'getBalanceByAddress'回报,呼叫仍在进行中,所以结果尚未公布。您必须在承诺链末尾追加结果处理。 – spectras

+0

如何尝试一种不同的方法:) getBalanceByAddress(....)。然后((结果)=> { 如果(结果> 228){ 的console.log( '一切OK') }其他{ console.log('insufficient funds') } }) getBalanceByAddress(addressFrom){ var _this = this; return _this.web3.eth.getBalance(addressFrom) } – orangespark

回答

3

你需要等待它:

(async function(){ 
    if(await getBalanceByAddress() > 228){ 
    console.log('ALL ok'); 
    } else { 
    console.log('insufficient funds'); 
    } 
})() 
+0

谢谢我试试 –

+2

即使附加的重复链接已经回答了它,我仍然喜欢这个简单的解决方案(不滚动)+1。你仍然需要让OP知道这个解决方案的浏览器支持! – gurvinder372