我试图从以太坊web3函数中获取帐户信息,但它返回Promises,我不知道如何使用Async / Await语法调用其中的值!
以下是代码:
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();
结果:
Promise { <pending> }
然后我尝试了:
web3.eth.accounts[0];
accounts();
结果:
Promise { <pending> }
然后我试图实现其他人标记为异步问题的重复答案的解决方案,但即使应用结果,我仍然得到Promise:
(async() => {
try {
let bal = await balance();
console.log(bal);
}
catch (e) {
//whatever
}
}
)();
返回:
Promise { <pending> }