I am making API calls wrapped in a timeout method that times out in the time that I define in the method call
function timeout(promise: Promise<Response>, ms: number): Promise<Response> {
return new Promise((resolve, reject)=>{
()=>setTimeout(()=>{
reject(new Error('Request timed out'))
}, ms)
return promise.then(resolve, reject)
})
}
I am then calling the timeout function in my code with a static timeout like:
timeout(fetch(url),5000)
I want to adjust the timeouts dynamically based on internet speed of the user. I am not sure what would be the best way to do it. Currently, I am using netinfo library to see if the user is connected and setting a timeout multiplier in redux in the addEventListener() method provided by netinfo. But that listener is only called if the internet is disconnected and reconnected or changed from wifi to data.
I am looking for advise on what would be the best practice to implement something like this where I can change the timeout multiplier more often without calling it too much.