Skip to content

Instantly share code, notes, and snippets.

@adelpro
Created December 26, 2023 09:08
Show Gist options
  • Select an option

  • Save adelpro/e4fd824579a87947b62a00d988d974ec to your computer and use it in GitHub Desktop.

Select an option

Save adelpro/e4fd824579a87947b62a00d988d974ec to your computer and use it in GitHub Desktop.
Subscribe
async function subscribe() {
let response = await fetch("/subscribe");
if (response.status == 502) {
// Status 502 is a connection timeout error,
// may happen when the connection was pending for too long,
// and the remote server or a proxy closed it
// let's reconnect
await subscribe();
return
}
if (response.status != 200) {
// An error - let's show it
showMessage(response.statusText);
// Reconnect in one second
await new Promise(resolve => setTimeout(resolve, 1000));
await subscribe();
return
}
// Get and show the message
let message = await response.text();
showMessage(message);
// Call subscribe() again to get the next message
await subscribe();
}
subscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment