If I try to get the result of a constant function call with:
let gameCountAwait = await contract.getGameEntry(0);
it fails with:
Uncaught SyntaxError: await is only valid in async function
Whereas if using .then
it works:
let gameCount = contract.getGameEntry(0).then(function(res,err){console.log(res[2]);});
- Ethers.js Version: 4.0.39
- Provider: Metamask
- Network: Ropsten
Here is the whole code in case that matters:
const ethers = require('ethers');
// The Contract interface
let abi = [
"event GameResult(bool won)",
"function lottery(uint8 guess) returns (bool value)",
"function getGameCount() view returns (uint value)",
"function getGameEntry(uint index) public view returns(address addr, uint amountBet, uint8 guess, bool winner, uint ethInJackpot)"
];
// Connect to the network
const provider = new ethers.providers.Web3Provider(web3.currentProvider);
let contractAddress = "0x7f8b9483b79f735C34820497A1a7f9FB82C9224b";
let contract = new ethers.Contract(contractAddress, abi, provider);
let gameCount = contract.getGameEntry(0).then(function(res,err){console.log(res[2]);}); //Works!
// let gameCountAwait = await contract.getGameEntry(0); //Does NOT work!
console.log(gameCount);
First time ever playing around with Ethers.js. Hope it is a valid alternative to web3.js. Thanks for your support!