@dyaa/async-retry
![wercker status wercker status](https://app.wercker.com/status/0171c17c9f92eaaaf5885d42c44b73a5/s/master)
Table of Contents
Problem
We needed a utility that helps us to retry an async function on it's Failure/Rejection and to be able to control the number of retries and the time interval between each retry also to block the retries in case of some array with http status codes provided!
Installation
yarn add @dyaa/async-retry
Or if you prefer npm!
npm i @dyaa/async-retry
Usage
import asyncRetry from '@dyaa/async-retry';
asyncRetry
expects two parameters
- The first parameter is a function that returns a
Promise
. - The second parameter is an options object (all of them are optional).
Options | Type | Optional? | Default | Description |
---|
retries | number | optional | 5retries | number of retries |
interval | number | optional | 5000ms | base interval between retries |
dontRetry | function | optional | () => false | A function that returns a boolean value. This boolean value is to check if we want the retry to continue or not |
onComplete | function | optional | null | . |
onFailure | function | optional | null | . |
onRetry | function | optional | null | . |
Example
import asyncRetry from '@dyaa/async-retry';
const fn = () => dispatch(initRealtimeService());
asyncRetry<ResolvedType, NetworkError>(fn, {
retries: 2,
interval: 2000,
dontRetry: (e: NetworkError) => [404, 503].includes(e.status),
}).then().catch();
The example above says that we gonna retry two times the function in case of it's Failure/Rejection with base interval of 2000
but if we got status codes 404
or 503
we won't retry at all.
Backoff formula
Inspiration
Thanks
License
MIT License © dyaa