XMLHttpRequest with cancel, timeout and retry option I've made the following code-snippet, trying to achieve a replacement for Fetch Which have the abortion, timeout and retry options.

It worked for me, but i'm not sure if it is a good idea to do so or not. Any better ideas?



const customXmlHttpRequest = (url, opts={}, cancel={}, timeoutMS = 30000) =&gt; {

  return new Promise((resolve, reject)=&gt;{

    var xhr = new XMLHttpRequest();
    xhr.open(opts.method || 'get', url);
    xhr.timeout = timeoutMS;
    for (var k in opts.headers || {}){
      xhr.setRequestHeader(k, opts.headers[k]);
    }
    xhr.onload = e =&gt; resolve(e.target);
    cancel.abort = () =&gt; {
      xhr.abort();
      reject("Cancelled")
    };
    xhr.onerror = reject;
    xhr.ontimeout = () =&gt; {
      reject("Timeout")
    }
    xhr.send(opts.body);

  });

}

const FetchWithRetry = (url, opts={}, retry=3, retryDelay=1000, cancel={}, timeoutMS = 30000) =&gt; {
  return new Promise((resolve, reject)=&gt;{
    var counter = 0;

    const FetchWithTimeout = ()=&gt;{
      if(counter &lt; retry){
        counter++
        customXmlHttpRequest(url, opts, cancel={}, timeoutMS = 30000)
        .then((res)=&gt;{
          resolve(res)
        })
        .catch((e)=&gt;{
          if(e === "Cancelled"){
            reject(e)
          } else if(e === "Timeout"){
            ///retry
            setTimeout(()=&gt;{
              FetchWithTimeout()
            }, retryDelay)
          } else if(String(e).includes("Network request failed")){
            /// retry
            setTimeout(()=&gt;{
              FetchWithTimeout()
            }, retryDelay)
          } else {
            reject(e)
          }
        })
      }
    }
    FetchWithTimeout()
  })
}
First of all, there's already a fetch function that's already available in most modern browsers. If you care about IE, there's also a polyfill for it. With that out of the way, we can ignore the first part of your code and focus on the second part of the code.

While it's perfectly fine JS, having very long argument lists is horrible. As a developer, you have to evaluate which arguments take priority and come first. As a consumer, you have to remember which ones come first. I suggest turning your options into an object. You can use Object.assign to merge a defaults object.

With regards to your control flow, you can simplify the retry routine by simply calling fetchWithRetry recursively, passing it the retries remaining. Since this operates on just promises and promises cannot be cancelled, this version's abort bails out when abort is called during the delay or after the request, not immediately.

const fetchWithRetry = (userOptions) =&gt; {
  let abort = false;
  const options = {
    url: '',
    options: {},
    cancel: {},
    retries: 3,
    retryDelay: 1000,
    ...userOptions
  };

  // Add an abort to the cancel object.
  cancel.abort = () =&gt; { abort = true };

  // Abort or proceed?
  return abort ? Promise.reject('aborted') : fetch(options.url).then(response =&gt; {

      // Reject because of abort
    return abort ? Promise.reject('aborted')

      // Response is good 
      : response.ok ? Promise.resolve(response.text()) 

      // Retries exceeded
      : !options.retries ? Promise.reject('retries exceeded')

      // Retry with one less retry
      : new Promise((resolve, reject){
          setTimeout(() =&gt; {

            // We use the returned promise's resolve and reject as
            // callback so that the nested call propagates backwards.
            fetchWithRetry({ ...options, retries: retries - 1 }).then(resolve, reject);
          }, options.retryDelay);

        });
  });
}