Create fault tolerant promises that retry upon failure according to a retry strategy.
The implementation of the retry strategies reuses code from the node-retry project.
Install retrying-promise
:
npm install retrying-promise
Import retrying-promise
:
const retryPromise = require('retrying-promise');
The constant retryPromise
is a function that returns a promise. Use this function similar to how you call the new Promise()
constructor, but pass it a function that takes four arguments: resolve
, retry
, reject
and attempt
.
var promise = retryPromise(function (resolve, retry, reject, attempt) {
console.log(`current attempt: ${attempt}`);
resolve(result); // the promise resolves normally
retry(error); // the promise failed and a retry may be attempted
reject(error); // the promise failed and no retry should be attempted
});
Call the resolve
function when the promise resolves normally.
Call the retry
function when the promise failed and a retry may be attempted.
Call the reject
function when the promise failed and no retry should be attempted.
Install Mocha.
sudo npm install -g mocha
Run tests:
npm test
Author: Wouter Van den Broeck
Copyright: 2016
Returns a promise that conditionally tries to resolve multiple times, as specified by the retry policy.
Kind: Exported function
Param | Type | Description |
---|---|---|
[options] | retryPolicy |
Either An object that specifies the retry policy. |
executor | retryExecutor |
A function that is called for each attempt to resolve the promise. |
Get a timeout value in milliseconds.
Kind: inner method of module.exports
Returns: number
- The timeout value in milliseconds.
Param | Type | Description |
---|---|---|
attempt | number |
The attempt count. |
opts | Object |
The options. |
An object that specifies the retry policy.
Kind: inner typedef of module.exports
Properties
Name | Type | Default | Description |
---|---|---|---|
retries | number |
10 |
The maximum amount of times to retry the operation. |
factor | number |
2 |
The exponential factor to use. |
minTimeout | number |
1000 |
The number of milliseconds before starting the first retry. |
maxTimeout | number |
Infinity |
The maximum number of milliseconds between two retries. |
randomize | boolean |
false |
Randomizes the timeouts by multiplying with a factor between 1 to 2. |
The function that is called for each attempt to resolve the promise.
Kind: inner typedef of module.exports
Param | Type | Description |
---|---|---|
resolveFn | function |
To be called when the promise resolves normally. |
retryFn | function |
To be called when the promise failed and a retry may be attempted. |
[rejectFn] | function |
To be called when the promise failed and no retry should be attempted. |
attempt | number |
The current attempt number. |
© 2016, Wouter Van den Broeck