A more functional retry function
npm install retry-fn
var retryFn = require('retry-fn')
var retry = retryFn.bind(null, {retries: 3, timeout: retryFn.fib(75)})
retry(unreliableFunction, function (err, result) {
// ...
})
retry-fn
exports a function that takes 3 arguments:
-
optional
Number||Object
, number of times to retry or an options object. Defaults to {retries: 3, timeout: 0}.{retries: Number, timeout: Number||Function}
:retries
number of times to retry before calling the callback with an errortimeout
a function taking the retry count, returning the number of milliseconds before attempting again. If anything else is passed it will be wrapped in a function and return that value on each call. Eg. pass a Number for "equally" spaced retries.
-
Function to attempt. Expecting the format
function(callback) { /* ... */ }
. If your function takes any other arguments, consider binding it or wrapping it. Seeexample/hyperquest.js
-
Callback which is called on success or when all retries have been exhausted.
Comes with three timeout functions builtin:
retry.fib(factor)
, returns a function for generating the Fibonacci sequence. Withfactor=100
: 100, 100, 200, 300, 500, 800, 1300retry.leo(factor)
, returns a function for generating the Leonardo sequence. Withfactor=100
: 100, 100, 200, 300, 500, 900, 1500retry.exp(factor)
, returns a function for generating a sequence according tofactor^n
. Withfactor=25
: 1, 25, 625, 15625