Promise Retry
Abstraction for exponential and custom retry strategies to failed promises.
Retrying made simple and easy. \o/
Installation
Using yarn:
yarn add promise-fn-retry
Using npm:
npm i --save promise-fn-retry
Usage
Simple Request
import fetch from 'node-fetch';
import retry from 'promise-fn-retry';
const requestUser = () => {
const promiseFn = () => fetch('https://api.github.com/users/14');
return retry(promiseFn)
.then(res => res.json());
};
export default requestUser;
Using options param
import fetch from 'node-fetch';
import retry from 'promise-fn-retry';
const requestUser = () => {
const promiseFn = () => fetch('https://api.github.com/users/14');
const options = {
times: 3,
initialDelay: 100,
};
return retry(promiseFn, options)
.then(res => res.json());
};
export default requestUser;
API
retry(promiseFn : Function, [options : Object]) => Promise
Options
These are the available config options for retrying. Only promiseFn is required. If an object isn't provided, the lib will use the default options.
{
times: 3,
initialDelayTime: 200,
onRetry: (error) => {
console.log(error);
sendToSentry(error);
sendToKibana(error);
},
shouldRetry: (error) => {
console.log(error);
return (error.message === 'FAILED_AUTH');
}
}
Delay strategy
Each retry doubles the current delay.
- The first delay uses the
initialDelayTime
option, like 100ms
. - The second uses
200ms
(100 * 2). - The third uses
400ms
...
License
Licensed under the MIT License, Copyright © 2018-present Felippe Maurício.