I am trying to fetch the balance of an address by web3 and node from Ropsten network with the commands below:

var Test = require('web3')
var url = 'https://ropsten.infura.io/v3/...'
var web3 = new Test(url)
var address = 'A Public Address'
web3.eth.getBalance(address, (err, bal) => {balance = bal})

The result is :


It was working and all of sudden this happened.


getBalance is asynchronous, so you can't assign a variable in the callback and expect that variable to be set in synchronous code after that.

You can move any code that requires the balance into the callback of getBalance, e.g.:

web3.eth.getBalance(address, (err, bal) => {
  if (err) {
    // Do something with the error
    return console.error(err);


You can read more about (async) callbacks here.

  • Got it and fixed it. Thanks! – Armin Sep 23 '20 at 4:11

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.