promise-retryer ![Build Status](https://camo.githubusercontent.com/d54d0999795a637a446c34b3ecaedb4ff96150dfc44fb844cc8bebe10a89dd10/68747470733a2f2f7472617669732d63692e6f72672f69636f6465666f726c6f76652f70726f6d6973652d726574727965722e706e673f6272616e63683d6d6173746572)
THIS IS DEPRECATED, PLEASE USE RTRY
provides an easy way to make promises retry based on exceptions or result validation
install
npm install promise-retryer
params
- delay (can be a number or function that returns a number, if its a function it gets called with an attempt argument)
- maxRetries (the maximum amount of retries)
- validate (validation promise)
- onError (callback function on errors, gets called with an error object and an attempt argument)
- onAttempt (callback function on attempts, gets called with an attempt argument)
- debug (enable verbose logging)
- name (this is useful for debugging purposes)
examples
heres a basic retry example
var PromiseRetryer = require('promise-retryer')(Promise);
PromiseRetryer.run({
delay: 1000,
maxRetries: 5,
promise: function (attempt) {
return makeAsyncRequest(); // returns a promise
}
}).then(
function (response) {
console.log(response);
},
function (error) {
console.log(error);
}
);
and heres a little more advanced example with validation and custom delays
var PromiseRetryer = require('promise-retryer')(Promise);
PromiseRetryer.run({
delay: function (attempt) {
return attempt * 1000;
},
maxRetries: 5,
promise: function (attempt) {
return makeAsyncRequest(); // returns a promise
},
validate: function (response, attempt) {
return new Promise(function (resolve, reject) {
if (typeof response === 'object') {
resolve(response);
} else {
reject(new Error('response was not an object'));
}
});
}
}).then(
function (response) {
console.log(response);
},
function (error) {
console.log(error);
}
);