Created
December 26, 2023 09:08
-
-
Save adelpro/e4fd824579a87947b62a00d988d974ec to your computer and use it in GitHub Desktop.
Subscribe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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