How to get a value from a “Promise” object

二次信任 提交于 2019-12-20 03:56:21

问题


I started learning ethereum and web3js and noticed some functions on Web3js are asynchronous. What i want to achieve is get the account balance of a wallet and use the data for something else. My code below

function getAccountBalance2(address){
            var wei, balance
            //address = document.getElementById("addy").value
            return new Promise(function(resolve, reject){
                web3.eth.getBalance(address, function(error, wei){
                    if(error){
                        console.log("Error with address");
                    }else{
                        var balance = web3.fromWei(wei, "ether");
                        var bal = resolve(balance);
                        //console.log(bal);
                        console.log(balance.toNumber());
                        return balance.toNumber();
                    }
                });
            });
        }

and i am trying to use the returned value in this function below

function interTransfer(){
            var from, to, amount, fromWallet, toWallet
            from = document.getElementById("from").value
            to = document.getElementById("to").value
            amount = document.getElementById("amount").value

            if(isWalletValid(from) && isWalletValid(to)){
                fromWallet = getAccountBalance2(from);
                toWallet = getAccountBalance2(to);
            }else{
                console.log("Something is wrong")
            }

            console.log(fromWallet + " "+ toWallet)
        }

The output

How do i get the actual value and use it in the interTransfer() function


回答1:


You need to await the promised values. You can do this with another then call, and -- to avoid one request having to wait for the previous one to finish -- Promise.all:

function interTransfer(){
    // ...
    promise = Promise.all([getAccountBalance2(from), getAccountBalance2(to)])
        .then(function ([fromWallet, toWallet]) {
            console.log('from wallet', fromWallet, 'to wallet', toWallet); 
        });
    // ...
    return promise; // the caller will also need to await this if it needs the values
}

Or, with an async function and the await keyword:

function async interTransfer(){
    // ...
    [fromWallet, toWallet] = 
        await Promise.all([getAccountBalance2(from), getAccountBalance2(to)]);
    console.log('from wallet', fromWallet, 'to wallet', toWallet); 
    // ...
    return [fromWallet, toWallet]; // caller's promise now resolves with these values
}

Note that the return in the getBalance callback is useless, and you should probably call reject with a reason in the case of if(error).



来源:https://stackoverflow.com/questions/47601214/how-to-get-a-value-from-a-promise-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!