How to resolve Web3 promises objects?

Viewed 1672

I'm trying to get the account info from the Ethereum web3 functions, but it returns Promises and I don't know how to call the value inside them using Async/Await syntax!

Here is the code:

var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');;


var accounts = async function fetchAccounts() {
let accs = await web3.eth.getAccounts();
return accs;
}; 

async function balance () {
let bal = await web3.eth.getBalance('0x398E42Da3254a22e13AbDBB4C68998BCDb3e2F1D');
    return bal;
};


 balance();

Result:

Promise { <pending> }

Then I tried:

 web3.eth.accounts[0];

 accounts();

Result:

Promise { <pending> }

EDITED

Then I tried to implement the solution that others have flagged as a duplicate answer of the async issue but even applying the results, I still get Promise:

(async() => {
  try {
  let bal = await balance();
  console.log(bal);
      }
  catch (e) {
      //whatever
  }
}
)();

Returns:

Promise { <pending> }
0 Answers
Related