https://github.com/tiaanduplessis/tenacious-fetch
Tiny fetch API wrapper to add support for retries with linear & exponential backoff and timeouts (651 Bytes)
https://github.com/tiaanduplessis/tenacious-fetch
fetch fetch-wrapper retry timeout
Last synced: 2 months ago
JSON representation
Tiny fetch API wrapper to add support for retries with linear & exponential backoff and timeouts (651 Bytes)
- Host: GitHub
- URL: https://github.com/tiaanduplessis/tenacious-fetch
- Owner: tiaanduplessis
- License: mit
- Created: 2018-05-15T14:56:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-08T18:33:58.000Z (almost 3 years ago)
- Last Synced: 2025-05-22T12:48:57.609Z (5 months ago)
- Topics: fetch, fetch-wrapper, retry, timeout
- Language: JavaScript
- Homepage:
- Size: 1.17 MB
- Stars: 18
- Watchers: 1
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
# tenacious-fetch
[](https://npmjs.org/package/tenacious-fetch)
[](https://npmjs.org/package/tenacious-fetch)
[](https://github.com/RichardLitt/standard-readme)
[](https://npmjs.org/package/tenacious-fetch)
[](http://makeapullrequest.com)> Tiny fetch API wrapper to add support for retries with linear & exponential backoff and timeouts
## Table of Contents
- [tenacious-fetch](#tenacious-fetch)
- [Table of Contents](#table-of-contents)
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#license)## Install
This project uses [node](https://nodejs.org) and [npm](https://www.npmjs.com).
```sh
$ npm install tenacious-fetch
$ # OR
$ yarn add tenacious-fetch
```## Usage
```js
import tenaciousFetch from 'tenacious-fetch'const url = 'https://jsonplaceholder.typicode.com/posts/1'
const normalFetchConfig = {
method: 'GET',
headers: {
"Content-Type": "application/json charset=UTF-8"
},
// Others...
}const additionalTenaciousFetchConfig = {
fetcher: window.fetch, // Fetch implementation to use, default is window.fetch
retries: 3, // Number of retries, default is 1
retryDelay: 1000 * 3, // Delay in ms before retrying, default is 1000ms
onRetry: ({retriesLeft, retryDelay, response}) => console.log(retriesLeft, retryDelay, response),
retryStatus = [], // Status codes of response that should trigger retry e.g. [500, 404] or just "500".
// defaults to empty array
retryOnFatalError = true // If there a fatal request (no response status), we can choose
// to retry or not
timeout = 1000 * 15, // Timeout in ms before throwing a timeout error for the request.
// Defaults to no timeout (undefined).
factor: .5 // If factor is given, exponential backoff will be performed for retries, otherwise
// linear backoff is used
}const config = Object.assign({}, normalFetchConfig, additionalTenaciousFetchConfig)
tenaciousFetch(url, config).then(console.log).catch(console.error)
```## Contribute
1. Fork it and create your feature branch: `git checkout -b my-new-feature`
2. Commit your changes: `git commit -am 'Add some feature'`
3. Push to the branch: `git push origin my-new-feature`
4. Submit a pull request## License
MIT