Objective: Assign a global variable to the first indexed address returned in web3.eth.getAccounts(). I want this so I can use it in the rest of my code and simply reference a variable.
Problem: Promises. There's something I'm not getting.
Two ways I've tried this:
1) async + await
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
const getAccounts = async () => {
const accounts = await web3.eth.getAccounts();
return accounts;
};
let acct = getAccount()[0];
console.log('The first account is: ' + acct);
2) .then()
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
let acct = web3.eth.getAccounts().then(data => {return data[0];});
console.log('The first account is: ' + acct);
Neither seem to be working for me, and must be doing something wrong. Best I can return is "Promise { pending }"
Can anyone help explain how I can use the result of a promise elsewhere in my code?