Skip to content

Wrap a function to retry on specific errors.

License

Notifications You must be signed in to change notification settings

antongolub-forks/retryify

 
 

Repository files navigation

retryify NPM version Build Status Coverage Status Greenkeeper

Quickly and easily wrap functions to make them retry when they fail.

Installation

$ npm install retryify

Example

// create a new retryify wrapper with some options set.
const retryify = require('retryify')({
  retries: 5,
  timeout: 1000,
  factor: 2,
  errors: [RequestError, StatusCodeError],
  log: function(msg) { console.log(msg); },
});

// promisified request library
const request = require('request-promise');

// get will now retry each time it catches a RequestError or a
// StatusCodeError, it retries 5 times, or the request finally resolves
// successfully.
const get = retryify(function(url) {
  return request(url);
});

// or, add some custom options for this specific function
const post = retryify({
  retries: 10
}, function(url, data) {
  return request({
    uri: url,
    method: 'POST',
  });
});

// send the request, but retry if it fails.
get('http://google.com')
  .then(...)
  .catch(...);

retryify([options]) ⇒ function

Retry module setup function. Takes an options object that configures the default retry options.

Kind: global function
Returns: function - retryWrapper A decorator function that wraps a a function to turn it into a retry-enabled function.
Throws:

  • TypeError when function is passed instead of options object. To use retryify it first must be "constructed" by passing in an options object and the returned function is what is supposed to take the function to retry.
Param Type Default Description
[options] Options {} Optional configuration object

retryify~retryWrapper([innerOptions], fn) ⇒ function

retryify function decorator. Allows configuration on a function by function basis.

Kind: inner method of retryify
Returns: function - The wrapped function.

Param Type Description
[innerOptions] Options Optional configuration object. Same format as above.
fn function The function to wrap. Will retry the function if any matching errors are caught.

Options : Object

Kind: global typedef
Properties

Name Type Default Description
[options.retries] Number 3 Number of times to retry a wrapped function
[options.initialDelay] Number 0 Amount of time (ms) to wait before any function attempts
[options.timeout] Number 300 Amount of time (ms) to wait between retries
[options.factor] Number 2 The exponential factor to scale the timeout by every retry iteration. For example: with a factor of 2 and a timeout of 100 ms, the first retry will fire after 100 ms, the second after 200 ms, the third after 400 ms, etc.... The formula used to calculate the delay between each retry: timeout * Math.pow(factor, attempts)
[options.shouldRetry] function () => true Invoked with the thrown error, retryify will retry if this method returns true.
[options.log] function Logging function that takes a message as

About

Wrap a function to retry on specific errors.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%