Skip to content

Instantly share code, notes, and snippets.

View jaguardo's full-sized avatar

MichaelJ jaguardo

  • Veth Research Associates
View GitHub Profile
@mjm918
mjm918 / ServerReachable.js
Last active March 11, 2024 02:19
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;
}
@babakness
babakness / use-interval.ts
Created February 4, 2019 18:54
Typescript version of Dan Abramov's useInterval requiring non-null value for delay
import * as React from 'react'
const { useState, useEffect, useRef } = React
type IntervalFunction = () => ( unknown | void )
function useInterval( callback: IntervalFunction, delay: number ) {
const savedCallback = useRef<IntervalFunction| null>( null )