async function handleFetch(grid, url) { let response = await fetch(url) if (response.ok) { game = await response.json() // Update the HTML view. updateHtml(grid, 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, `longpoll`) // longpoll to wait for state change } }