Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vercel/async-retry
Retrying made simple, easy and async
https://github.com/vercel/async-retry
async await javascript retry
Last synced: 2 days ago
JSON representation
Retrying made simple, easy and async
- Host: GitHub
- URL: https://github.com/vercel/async-retry
- Owner: vercel
- License: mit
- Created: 2016-02-29T18:31:57.000Z (almost 9 years ago)
- Default Branch: main
- Last Pushed: 2023-07-19T03:46:53.000Z (over 1 year ago)
- Last Synced: 2024-12-05T19:37:37.083Z (7 days ago)
- Topics: async, await, javascript, retry
- Language: JavaScript
- Homepage: https://npmjs.com/async-retry
- Size: 636 KB
- Stars: 1,851
- Watchers: 54
- Forks: 53
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-github-star - async-retry
- awesome-star-libs - vercel / async-retry
README
# async-retry
Retrying made simple, easy, and async.
## Usage
```js
// Packages
const retry = require('async-retry');
const fetch = require('node-fetch');await retry(
async (bail) => {
// if anything throws, we retry
const res = await fetch('https://google.com');if (403 === res.status) {
// don't retry upon 403
bail(new Error('Unauthorized'));
return;
}const data = await res.text();
return data.substr(0, 500);
},
{
retries: 5,
}
);
```### API
```js
retry(retrier : Function, opts : Object) => Promise
```- The supplied function can be `async` or not. In other words, it can be a function that returns a `Promise` or a value.
- The supplied function receives two parameters
1. A `Function` you can invoke to abort the retrying (bail)
2. A `Number` identifying the attempt. The absolute first attempt (before any retries) is `1`.
- The `opts` are passed to `node-retry`. Read [its docs](https://github.com/tim-kos/node-retry)
- `retries`: The maximum amount of times to retry the operation. Default is `10`.
- `factor`: The exponential factor to use. Default is `2`.
- `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`.
- `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`.
- `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `true`.
- `onRetry`: an optional `Function` that is invoked after a new retry is performed. It's passed the `Error` that triggered it as a parameter.## Authors
- Guillermo Rauch ([@rauchg](https://twitter.com/rauchg)) - [Vercel](https://vercel.com)
- Leo Lamprecht ([@notquiteleo](https://twitter.com/notquiteleo)) - [Vercel](https://vercel.com)