6.0.0 • Published 2 years ago

retryify v6.0.0

Weekly downloads
354
License
MIT
Repository
github
Last release
2 years ago

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.
ParamTypeDefaultDescription
optionsOptions{}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.

ParamTypeDescription
innerOptionsOptionsOptional configuration object. Same format as above.
fnfunctionThe function to wrap. Will retry the function if any matching errors are caught.

Options : Object

Kind: global typedef Properties

NameTypeDefaultDescription
options.retriesNumber3Number of times to retry a wrapped function
options.initialDelayNumber0Amount of time (ms) to wait before any function attempts
options.timeoutNumber300Amount of time (ms) to wait between retries
options.factorNumber2The 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.shouldRetryfunction() => trueInvoked with the thrown error, retryify will retry if this method returns true.
options.logfunctionLogging function that takes a message as
6.0.0

2 years ago

5.1.0

2 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

4.0.0-beta.1

4 years ago

3.0.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.1.1

7 years ago

1.1.0

8 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago