async function handleFetch(grid, status, url) { let response = await fetch(url); if (response.ok) { game = await response.json(); // Update the HTML view. updateHtml(grid, game); // Update game status area to reflect winner / tie updateStatus(status, game); } if (!response.ok) { await wait(500) // wait a little if there is connection trouble } if (!response.ok || game.state == "waiting" || game.state == "playing" && !game.myturn) { await handleFetch(grid, status, `longpoll`); // longpoll to wait for state change } }