Skip to content

Instantly share code, notes, and snippets.

@mjm918
Last active March 11, 2024 02:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mjm918/b2e6c87da896dc7acff5d0b53cc74a7e to your computer and use it in GitHub Desktop.
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…
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