This is part of a simple blockchain game, (virusContract)
The problem is displayWinner() is being run before “winner” is returned,
causing winner to be undefined at the time of function call.
async handleForm(event) {
event.preventDefault();
// listen for winner()
var winner
this.props.virusContract.once('Winner', function(error, event) {
winner = event.returnValues.tokenId
console.log("find winner = " + winner)
})
await this.props.virusContract.methods.infect(this.state.myVirusID, this.state.enemyVirusID).send({ from: this.props.account })
await this.displayWinner(winner)
}
displayWinner(theWinner) {
console.log("And the winner is... " + theWinner);
}
How can I get displayWinner to wait for the “winner” to be defined please?