Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // import fetch from 'isomorphic-unfetch';
- const RETRIES = 5;
- /**
- * Example:
- * global.fetch = fetchWithRetry;
- */
- function fetchWithRetry(url, options) {
- const opts = options || {};
- // only retry if the request is GET or HEAD:
- if (/(get|head)/i.test(opts.method)) {
- return doFetch(url, opts, opts.retries || RETRIES);
- }
- return fetch(url, opts);
- }
- function doFetch(url, options, retriesRemaining) {
- return fetch(url, options).catch(err => {
- if (err.timedout) { // you have to figure out when you want to retry
- if (retriesRemaining) {
- return doFetch(url, options, retriesRemaining - 1);
- }
- }
- throw err;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement