Advertisement
Guest User

Untitled

a guest
May 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. // import fetch from 'isomorphic-unfetch';
  2.  
  3. const RETRIES = 5;
  4.  
  5. /**
  6. * Example:
  7. * global.fetch = fetchWithRetry;
  8. */
  9.  
  10. function fetchWithRetry(url, options) {
  11. const opts = options || {};
  12. // only retry if the request is GET or HEAD:
  13. if (/(get|head)/i.test(opts.method)) {
  14. return doFetch(url, opts, opts.retries || RETRIES);
  15. }
  16. return fetch(url, opts);
  17. }
  18.  
  19. function doFetch(url, options, retriesRemaining) {
  20. return fetch(url, options).catch(err => {
  21. if (err.timedout) { // you have to figure out when you want to retry
  22. if (retriesRemaining) {
  23. return doFetch(url, options, retriesRemaining - 1);
  24. }
  25. }
  26. throw err;
  27. });
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement