@songkick/promise-retry
Advanced tools
Comparing version 1.1.0 to 1.2.1
23
index.js
@@ -8,5 +8,13 @@ var factory = function (createExecutor) { | ||
}; | ||
} | ||
}; | ||
}; | ||
var OutOfRetriesError = function (settings, fn, errors) { | ||
this.message = 'Maximum retries count reached'; | ||
this.settings = settings; | ||
this.fn = fn; | ||
this.errors = errors; | ||
}; | ||
OutOfRetriesError.prototype = Object.create(Error.prototype); | ||
var promiseRetry = factory(function (fn, settings) { | ||
@@ -20,2 +28,6 @@ var failureCount = 0, | ||
var getDelay = typeof settings.delay === 'function' ? settings.delay : function(){ | ||
return settings.delay; | ||
}; | ||
function executor(resolve, reject) { | ||
@@ -32,3 +44,3 @@ return fn() | ||
executor(resolve, reject); | ||
}, settings.delay); | ||
}, getDelay(failureCount)); | ||
} | ||
@@ -41,9 +53,2 @@ }); | ||
var OutOfRetriesError = function (settings, fn, errors) { | ||
this.message = 'Maximum retries count reached'; | ||
this.settings = settings; | ||
this.fn = fn; | ||
this.errors = errors; | ||
}; | ||
OutOfRetriesError.prototype = Object.create(Error.prototype); | ||
@@ -50,0 +55,0 @@ promiseRetry.OutOfRetriesError = OutOfRetriesError; |
{ | ||
"name": "@songkick/promise-retry", | ||
"version": "1.1.0", | ||
"version": "1.2.1", | ||
"description": "Retry a function until its returned promise succeeds", | ||
@@ -11,3 +11,4 @@ "repository": { | ||
"scripts": { | ||
"test": "tap test.js" | ||
"test": "tap test.js --cov --coverage-report=lcov", | ||
"report-coverage": "codeclimate-test-reporter < coverage/lcov.info" | ||
}, | ||
@@ -22,2 +23,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"codeclimate-test-reporter": "^0.1.1", | ||
"tap": "^1.4.1" | ||
@@ -24,0 +26,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# promise-retry [![Build Status](https://travis-ci.org/songkick/promise-retry.svg)](https://travis-ci.org/songkick/promise-retry) | ||
# promise-retry [![Build Status](https://travis-ci.org/songkick/promise-retry.svg)](https://travis-ci.org/songkick/promise-retry) [![Code Climate](https://codeclimate.com/github/songkick/promise-retry/badges/gpa.svg)](https://codeclimate.com/github/songkick/promise-retry) [![Test Coverage](https://codeclimate.com/github/songkick/promise-retry/badges/coverage.svg)](https://codeclimate.com/github/songkick/promise-retry/coverage) | ||
@@ -40,4 +40,21 @@ Retry a function until its returned promise succeeds | ||
`delay`: the delay between retries. Does not apply on initial call. | ||
`delay`: the delay between retries or a `function(retryIndex){}` returning the delay. Does not apply on initial call. If a function is passed, it will receive the retry index as first argument (`1, 2, 3 ...`). | ||
### Example: | ||
```js | ||
var retryWithIncreasingDelay = retryPromise({ retries: 10, delay: function(retryIndex) { | ||
return 100 * retryIndex; | ||
}); | ||
/* time line: | ||
0 > initial fail | ||
0 + 100 > first retry | ||
100 + 200 > second retry | ||
300 + 300 > third retry | ||
600 + 400 > fourth... | ||
``` | ||
## Composition | ||
@@ -44,0 +61,0 @@ |
52
test.js
@@ -27,3 +27,3 @@ var tap = require('tap'); | ||
t.equal(result, 'success', 'result should be original promise result'); | ||
}).catch(function (err) { | ||
}).catch(function () { | ||
t.bailout('the promise was unexpectedly rejected'); | ||
@@ -43,4 +43,3 @@ }); | ||
t.equal(succeedTheThirdTime.calls, 3, 'it kept calling the library'); | ||
}).catch(function (err) { | ||
console.log(err); | ||
}).catch(function () { | ||
t.bailout('the promise was unexpectedly rejected'); | ||
@@ -68,3 +67,3 @@ }); | ||
retryPromise({retries: 10})(nope)().then(function (err) { | ||
retryPromise({retries: 10})(nope)().then(function () { | ||
t.bailout('the promise was unexpectedly resolved'); | ||
@@ -82,3 +81,3 @@ }).catch(function (error) { | ||
tap.test('delay option', function (t) { | ||
tap.test('delay option as number', function (t) { | ||
@@ -105,2 +104,43 @@ t.plan(3); | ||
tap.test('delay option as function', function(t){ | ||
t.plan(5); | ||
var tick = 25; | ||
var succeedTheThirdTime = succeedAfter(3); | ||
function retriesTimesTickTimesTwo(retries) { | ||
var delay = retries * tick * 2; | ||
return delay; | ||
} | ||
// t-0 request > fail | ||
// t-0+2=2 request > fail | ||
// t-2+4=6 request > fail | ||
// t-6+8=14 request > success | ||
retryPromise({retries: 3, delay: retriesTimesTickTimesTwo})(succeedTheThirdTime)().then(function () { | ||
t.equal(succeedTheThirdTime.calls, 3, 'function should be called after specified delay'); | ||
}); | ||
setTimeout(function afterFirstCall() { | ||
t.equal(succeedTheThirdTime.calls, 1, 'function must be called right away the first time'); | ||
}, 1 * tick); | ||
setTimeout(function afterSecondCall() { | ||
t.equal(succeedTheThirdTime.calls, 2, 'function should be retried after 2 ticks'); | ||
}, 3 * tick); | ||
setTimeout(function beforeThirdCall() { | ||
t.equal(succeedTheThirdTime.calls, 2, 'function must not have been called before 2 + 4 ticks the third time'); | ||
}, 5 * tick); | ||
setTimeout(function afterThirdCall() { | ||
t.equal(succeedTheThirdTime.calls, 3, 'function should be retried after 2 + 4 ticks'); | ||
}, 7 * tick); | ||
setTimeout(function afterEverything() { | ||
t.equal(succeedTheThirdTime.calls, 4, 'function should be called 4 times at t-14'); | ||
}, 15 * tick); | ||
}); | ||
tap.test('composition', function (t) { | ||
@@ -129,3 +169,3 @@ | ||
t.equal(res, 'ok', 'the initial result was not returned'); | ||
}).catch(function (err) { | ||
}).catch(function () { | ||
t.bailout('the global promise was rejected'); | ||
@@ -132,0 +172,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12418
8
184
95
2