README
Retry callback based or async function
Example
- Operation
const BusyService = {
call_times : 0,
getHelloAtEach3Times : function (must_be_hi, callback) {
this.call_times++;
if (this.call_times % 3 === 0) {
return callback(null, 'hello');
}
let BusyError = new Error('Service is busy');
BusyError.status = 503;
return callback(BusyError);
},
asyncGetHelloAtEach3Times : async function (must_be_hi) {
this.call_times++;
if (this.call_times % 3 === 0) {
return 'hello';
}
let BusyError = new Error('Service is busy');
BusyError.status = 503;
throw BusyError;
}
};
- Async Retry
const { asyncRetry, Timeout } = require('x-retry');
it ('should retry a async function ok after three times', async () => {
let message = await asyncRetry({
func : BusyService.asyncGetHelloAtEach3Times,
thisArg : BusyService,
args : ['hi'],
isRetry : (error) => !(error.status >= 400 && error.status < 500),
maxRetry : 3,
timeout : Timeout({ minTimeout : 100, maxTimeout : 10000 })
});
assert.equal(message, 'hello');
});
- Callback Retry
const { callbackRetry, Timeout } = require('x-retry');
it ('should retry a callback function ok after three times', (done) => {
callbackRetry({
func : BusyService.getHelloAtEach3Times,
thisArg : BusyService,
args : ['hi'],
isRetry : (error) => !(error.status >= 400 && error.status < 500),
maxRetry : 3,
timeout : Timeout({ minTimeout : 100, maxTimeout : 10000 }),
callback : (err, message) => (!err && message === 'hello') ? done() : done(err)
});
});
- See more cases in test file
Functions
- asyncRetry(options) ⇒
Promise.<any>
Retry an async function
- callbackRetry(options) ⇒
void
Retry an callback based function
- Timeout([options]) ⇒
function
Create function that generate timeout by exponential backoff algorithm
Promise.<any>
asyncRetry(options) ⇒ Retry a async function
Kind: function
Returns: Promise.<any>
- result of async func
Throws:
- ERR_REACHED_MAX_RETRY
- ERR_CANNOT_RETRY when not reached max retry but isRetry() return false.
Param | Type | Default | Description |
---|---|---|---|
options | object |
||
options.func | function |
async function to retry | |
[options.thisArg] | object |
this pointer apply to function | |
[options.args] | Array.<any> |
arguments of function | |
[options.isRetry] | function |
(error) => boolean | |
[options.maxRetry] | number |
3 |
max retry times |
[options.timeout] | number | function |
delay between retry operation, default is generated by Timeout. if is function, must match interface : (retry_count, maxRetry, logs) => number | |
[options.activity] | string |
what is this activity name ? default is func.name | |
[options.actor] | string |
who do this activity ? default is thisArg.name |
void
callbackRetry(options) ⇒ Retry an callback based function
Kind: function
Errors: :
- ERR_REACHED_MAX_RETRY
- ERR_CANNOT_RETRY when not reached max retry but isRetry() return false.
Param | Type | Default | Description |
---|---|---|---|
options | object |
||
options.func | function |
callback based function to retry | |
[options.thisArg] | object |
this pointer apply to function | |
[options.args] | Array.<any> |
arguments of function | |
options.callback | function |
callback of function | |
[options.isRetry] | function |
(error) => boolean | |
[options.maxRetry] | number |
3 |
max retry times |
[options.timeout] | number | function |
200 |
delay between retry operation, , default is generated by Timeout. if is function, must match interface : (retry_count, maxRetry, logs) => number |
[options.activity] | string |
what is this activity name ? default is func.name | |
[options.actor] | string |
who do this activity ? default is thisArg.name |
function
Timeout([options]) ⇒ Create function that generate timeout by exponential backoff algorithm
Kind: function
Returns: function
- generate timeout
Param | Type | Default |
---|---|---|
[options] | Object |
|
[options.minTimeout] | Number |
20 |
[options.maxTimeout] | Number |
Infinite |
[options.factor] | Number |
2 |
[options.randomize] | Boolean |
true |
number
Timeout~generateTimeout([retryCount]) ⇒ Generate timeout = Math.min(random * minTimeout * Math.pow(factor, retryCount), maxTimeout)
Kind: inner method of Timeout
Returns: number
- timeout
Param | Type | Default |
---|---|---|
[retryCount] | number |
1 |