Fetch-retry

fetch-retry

Adds retry functionality to the Fetch API.

fetch-retry in about 88 lines of code provides an extension to the fetch API to accept retries, retryDelay, and retryOn on the options object, when omitted will default to 3 retries, a 1000ms retry delay, and to retry only on network errors.

Get it: npm install fetch-retry

Smaple usages:

1
const  fetch = require('fetch-retry');
1
2
3
4
5
6
7
8
9
10
11
fetch(url, {
    retries: 3,
    retryDelay: 1000
  })
  .then(function(response) {
    return response.json();
  })
  .then(function(json) {
    // do something with the result
    console.log(json);
  });
1
2
3
4
5
6
7
8
9
10
11
// Retry on 503
fetch(url, {
    retryOn: [503]
  })
  .then(function(response) {
    return response.json();
  })
  .then(function(json) {
    // do something with the result
    console.log(json);
  });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Custorm retry
fetch(url, {
    retryOn: function(attempt, error, response) {
      // retry on any network error, or 4xx or 5xx status codes
      if (error !== null || response.status >= 400) {
        console.log(`retrying, attempt number ${attempt + 1}`);
        return true;
      }
    })
    .then(function(response) {
      return response.json();
    }).then(function(json) {
      // do something with the result
      console.log(json);
    });
}

GIF FTW!

fetch-retry

Suggest a module

Comments