2017-12-27 163 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