Product
Introducing Java Support in Socket
We're excited to announce that Socket now supports the Java programming language.
fetch-retry
Advanced tools
The fetch-retry npm package is an extension of the native fetch API that adds the ability to automatically retry a failed HTTP request. This is particularly useful for dealing with transient network issues or temporary server-side errors. It allows developers to specify the number of retries, the retry delay, and other retry policies.
Automatic retries for failed requests
This feature allows fetch requests to be automatically retried a specified number of times with a delay between each attempt. The code sample shows how to wrap the native fetch with fetch-retry to make a GET request that retries up to 3 times with a 1-second delay between retries.
fetch = require('fetch-retry')(require('node-fetch'));
fetch('https://api.example.com', {
retries: 3,
retryDelay: 1000
}).then(function(response) {
return response.json();
}).then(function(json) {
console.log(json);
}).catch(function(error) {
console.error(error);
});
Customizable retry on function
This feature allows developers to define a custom function to determine whether a request should be retried based on the attempt number, error, and response. The code sample demonstrates a custom retryOn function that retries the request if an error occurs or if the response status code is 500 or greater.
fetch = require('fetch-retry')(require('node-fetch'));
fetch('https://api.example.com', {
retries: 4,
retryDelay: 1000,
retryOn: function(attempt, error, response) {
if (error !== null || response.status >= 500) {
return true;
}
return false;
}
}).then(function(response) {
return response.json();
}).then(function(json) {
console.log(json);
}).catch(function(error) {
console.error(error);
});
axios-retry is a package that provides similar retry functionality for Axios, a popular HTTP client. It allows for configuring retry conditions, delay strategies, and the number of retries. Unlike fetch-retry, which is built for the fetch API, axios-retry is specifically designed to work with Axios.
got is a more comprehensive HTTP request library that includes built-in retry functionality among many other features. It supports retries with a more extensive set of options and strategies compared to fetch-retry. Got is a standalone package, whereas fetch-retry is an extension of the native fetch API.
superagent-retry extends the superagent library to add retry capabilities. It is similar to fetch-retry in that it adds retry functionality to an existing HTTP request library, but it is designed for superagent instead of fetch.
Adds retry functionality to the Fetch API.
It wraps any fetch
API package (eg: isomorphic-fetch, cross-fetch, isomorphic-unfetch, or Node.js native's fetch implementation) and retries requests that fail due to network issues. It can also be configured to retry requests on specific HTTP status codes.
npm install fetch-retry --save
fetch-retry
is used the same way as fetch
, but also accepts retries
, retryDelay
, and retryOn
on the options
object.
These properties are optional, and unless different defaults have been specified when requiring fetch-retry
, these will default to 3 retries, with a 1000ms retry delay, and to only retry on network errors.
const originalFetch = require('isomorphic-fetch');
const fetch = require('fetch-retry')(originalFetch);
// fetch-retry can also wrap Node.js's native fetch API implementation:
const fetch = require('fetch-retry')(global.fetch);
fetch(url, {
retries: 3,
retryDelay: 1000
})
.then(function(response) {
return response.json();
})
.then(function(json) {
// do something with the result
console.log(json);
});
or passing your own defaults:
const originalFetch = require('isomorphic-fetch');
const fetch = require('fetch-retry')(originalFetch, {
retries: 5,
retryDelay: 800
});
fetch-retry
uses promises and requires you to polyfill the Promise API in order to support Internet Explorer.
The default behavior of fetch-retry
is to wait a fixed amount of time between attempts, but it is also possible to customize this by passing a function as the retryDelay
option. The function is supplied three arguments: attempt
(starting at 0), error
(in case of a network error), and response
. It must return a number indicating the delay.
fetch(url, {
retryDelay: function(attempt, error, response) {
return Math.pow(2, attempt) * 1000; // 1000, 2000, 4000
}
}).then(function(response) {
return response.json();
}).then(function(json) {
// do something with the result
console.log(json);
});
The default behavior of fetch-retry
is to only retry requests on network related issues, but it is also possible to configure it to retry on specific HTTP status codes. This is done by using the retryOn
property, which expects an array of HTTP status codes.
fetch(url, {
retryOn: [503]
})
.then(function(response) {
return response.json();
})
.then(function(json) {
// do something with the result
console.log(json);
});
The retryOn
option may also be specified as a function, in which case it will be supplied three arguments: attempt
(starting at 0), error
(in case of a network error), and response
. Return a truthy value from this function in order to trigger a retry, any falsy value will result in the call to fetch either resolving (in case the last attempt resulted in a response), or rejecting (in case the last attempt resulted in an error).
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);
});
The retryOn
option may also be used with async and await for calling asyncronous functions:
fetch(url, {
retryOn: async function(attempt, error, response) {
if (attempt > 3) return false;
if (error !== null) {
var json = await response.json();
if (json.property !== undefined) {
return true;
}
}
})
.then(function(response) {
return response.json();
}).then(function(json) {
// do something with the result
console.log(json);
});
FAQs
Extend any fetch library with retry functionality
The npm package fetch-retry receives a total of 3,457,516 weekly downloads. As such, fetch-retry popularity was classified as popular.
We found that fetch-retry demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.
Security News
Deno 2.0 is now available with enhanced package management, full Node.js and npm compatibility, improved performance, and support for major JavaScript frameworks.