Skip to content

Instantly share code, notes, and snippets.

@pradhul-dev
Last active November 5, 2019 10:18
Show Gist options
  • Save pradhul-dev/89266a9e113cdebaf8d6ec755e1e3dd6 to your computer and use it in GitHub Desktop.
Save pradhul-dev/89266a9e113cdebaf8d6ec755e1e3dd6 to your computer and use it in GitHub Desktop.
React native's Netinfo gives connectivity status but not actually tells weather any URL is reachable or not
import { TimeoutError } from 'rxjs'
import { ajax } from 'rxjs/ajax'
import { catchError, map, timeout } from 'rxjs/operators'
/**
* Netinfo method for checking internet connection won't tell weather the internet is actually accessible or not
* make a test API call to verify
*/
export function isInternetAvailable() {
ajax('https://google.com').pipe(
timeout(100),
map(userResponse => console.log('Got response', userResponse)),
catchError(err => {
console.log('An Error occured', err)
if (err instanceof TimeoutError) {
console.log('Request Timeout!!')
}
return of(err)
}),
).subscribe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment