1

I'm trying to use promises instead of nested callbacks for better readabilty. When I use this code:

balanceUpdate: function() {

    MyWallet.deployed().then(function(instance) {
     return web3.eth.getBalance(instance.address);
    }).then(function(walletBalance) {
      document.getElementById("walletEther").innerHTML = web3.fromWei(walletBalance).toNumber();
      return web3.eth.getBalance(account);
    }).then(function(accountBalance) {
      document.getElementById("accountEther").innerHTML = web3.fromWei(accountBalance).toNumber();
    }).catch(function(error){
      console.error(error);
      throw "Can't get balances"
    })   
}

I got this error when Metamask is active:

Error: The MetaMask Web3 object does not support synchronous methods like eth_getBalance without a callback parameter. See https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#dizzy-all-async---think-of-metamask-as-a-light-client for details.

It works fine if I use a nested callback but I find it much less readable:

balanceUpdate: function() {

    MyWallet.deployed().then(function(instance) {

      web3.eth.getBalance(instance.address, function(error, result) {
        if (error) {
          console.error(error);
        } else {
          document.getElementById("walletEther").innerHTML = web3.fromWei(result.toNumber());
          web3.eth.getBalance(account, function(error, result) {
            if (error) {
              console.error(error);
            } else {
              document.getElementById("accountEther").innerHTML = web3.fromWei(result.toNumber());
            }
          }); // getbalance account
        }
      }); // getBalance instance.address
    }); // MyWallet.deployed
  }, // balanceUpdate

I've found this issue ("Promisify TxManager") on Metamask's github. Not sure if it's related.

Any suggestion how to avoid nested callbacks and be able to use Metamask the same time?

Even the samples on truffle sites are using promises. Is it possible that those samples won't work with Metamask?

5

The web3 object that is currently injected by MetaMask and other web3 browsers does not return promises.

The MetaMask issue you linked is not related to this.

Truffle has its own Contract library that returns promises, this works with MetaMask and other web3 browsers, that's why it returns promises but the pure web3 doesn't.

The web3 object itself is even going to be deprecated from Mist and MetaMask in the not-too distant future. That's one of the reasons that most of the web3 code samples you'll find show you to check for web3, and then recommend initializing your own web3 object with its web3.currentProvider object.

In the future, this provider will be the important, low-level piece that you use to initialize other libraries that meet your programming style.

A new cool library you might enjoy that works with MetaMask, uses its provider, but returns promises is ethjs, I recommend you check it out!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.