Last active
March 11, 2024 02:19
-
-
Save mjm918/b2e6c87da896dc7acff5d0b53cc74a7e to your computer and use it in GitHub Desktop.
React Native check if server is reachable. Sometimes the device shows that you are connected to internet but when you make a request to your server, you dont get any response. This handy code will ping a server and wait for response, if no response, it will alert the user. You can use this code on top of your fetch function. It will run in paral…
This file contains 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
export const isReachable = async () =>{ | |
const timeout = new Promise((resolve, reject) => { | |
setTimeout(reject, 5000, 'Request timed out'); | |
}); | |
const request = fetch('https://xxxxxx.com'); | |
try { | |
const response = await Promise | |
.race([timeout, request]); | |
return true; | |
} | |
catch (error) { | |
return alert('We are having some issue in connecting to EasySales server. Please check your internet connection'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment