I'm designing a simple browser based version of a popular game and I'm having trouble understanding how promises should be created by a button .onclick handler to deal with user input only at specific times. Here's the basic game logic:
<button id="button1">1</button>
<button id="button2">2</button>
<script>
var Game = {
ready_for_input: false,
game_over: false,
play_round: async function () {
//do some setup, check for a few initial win conditions, then......
if (round_not_yet_won) {
ready_for_input=true;
while (round_not_yet_won) {
await (//somehow get a promise that is fulfilled on button click from one of two buttons)
//when promise fulfilled by click, do some additional condition checking
if (win) {round_not_yet_won=false; ready_for_input=false; return;} else {//go back to while loop}
}
}
}
while (!Game.game_over){
Game.play_round();
}
//get buttons by id and addEventListener('click', handle_button_x())
function handle_button_x() {//create a promise maybe?}
</script>
I realized while debugging previously that the reason things weren't flowing logically was that I hadn't used async/await on the while loop, so it was sporadically hanging on certain initial conditions and not letting the page send the click event. Now i realize i need to let play_round() await the button click and stop blocking so the page can do its thing, but i'm not understanding how I link up the promise both to the play_round() await structure and the handle_button_x() handler. It seems wrong to my brain to do something like this, e.g.:
await handle_button_x();
because this will return a promise, but it seemingly won't be linked to a button press event call of the same function. Does my failure of understanding lie in how Promise.resolve() is called maybe?
Thanks for any help.