Hi campers, builinding a Simon game here.
Is it possible to use async and await in forloop like below? I am trying to play a sequence at a time that has set timeout.
I noticed that setTimeouts don’t return promises. Is there a good way to handle this?
playComputerSequences = async () => {
let { computerSequences, currentRound } = this.state
for (let i = 0; i < currentRound; i++) {
await this.playPad(computerSequences[i])
}
this.setState(prevState => {
return {
currentRound: prevState.currentRound + 1,
}
})
}
playPad = padName => {
const { pads, activePad } = this.state
pads.forEach(pad => {
if (pad.name === padName) {
pad.sound.play()
this.setState({
activePad: {
...activePad,
[pad.name]: true,
},
})
setTimeout(() => {
this.setState({
activePad: {
...activePad,
[pad.name]: false,
},
})
}, 250)
}
})
}