0

There are 10 Ethereum accounts in Ganache. I work on a Truffle project. Whenever i run the following codes in Nodejs:

var Web3 = require('web3');
var web3 = new Web3("ws://localhost:8545");
var acc= Promise.resolve(web3.eth.getAccounts());
acc.then(function(ins){console.log(ins[0]);});

Address of the first account will be shown:

enter image description here

But i want to see that address in my browser. What is the appropriate code?

  • There is an appropriate solution code here. – Alireza Apr 12 at 11:48
  • There is an appropriate solution code here – Alireza Apr 12 at 11:50
1

In your working example, you are handling the result of getAccounts in an asynchronous manner:

web3.eth.getAccounts().then(console.log);

In your non-working example, you are wrongfully assuming that it runs synchronously to completion:

var acc=web3.eth.getAccounts();

Function web3.eth.getAccounts returns a Promise object which, when resolved, contains all the accounts which are unlocked on the node that your web3 object is connected to.

Resolve this promise, and you should be good.

| improve this answer | |
  • I am a beginner in programming and my knowledge about promise and JavaScript is low. Please clear more about resolve the promise. – Alireza Feb 10 at 9:31
  • @A.Kiakojouri: How hard is it, even for a beginner, to take a working example (which you already have!!!) and use it instead of the non-working example??? – goodvibration Feb 10 at 9:43

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.