promise-retry
Advanced tools
Comparing version 0.1.1 to 0.2.0
27
index.js
@@ -7,2 +7,4 @@ 'use strict'; | ||
var emptyFunc = function () {}; | ||
function promiseRetry(fn, options) { | ||
@@ -17,7 +19,13 @@ var operation = retry.operation(options); | ||
return fn(function (err) { | ||
if (operation.retry(err)) { | ||
return errcode('Retrying', 'EPROMISERETRY'); | ||
if (!err) { | ||
throw new Error('Retry called without an error'); | ||
} | ||
return operation.mainError(); | ||
if (err.code === 'EPROMISERETRY') { | ||
err = err.original; | ||
} | ||
throw errcode('Retrying', 'EPROMISERETRY', { | ||
original: err | ||
}); | ||
}); | ||
@@ -27,6 +35,13 @@ }); | ||
promise.then(resolve, function (err) { | ||
if (!err || err.code !== 'EPROMISERETRY') { | ||
reject(err); | ||
if (err && err.code === 'EPROMISERETRY') { | ||
err = err.original; | ||
if (operation.retry(err.original)) { | ||
throw err.original; | ||
} | ||
} | ||
}); | ||
reject(err); | ||
}) | ||
.done(emptyFunc, emptyFunc); | ||
}); | ||
@@ -33,0 +48,0 @@ }); |
{ | ||
"name": "promise-retry", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Retries a function that returns a promise, leveraging the power of the retry module.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -29,17 +29,16 @@ # node-promise-retry [![Build Status](https://travis-ci.org/IndigoUnited/node-promise-retry.svg?branch=master)](https://travis-ci.org/IndigoUnited/node-promise-retry) | ||
The `fn` function will receive a `retry` function as its first argument that should be called with an error whenever you want to retry `fn`. | ||
The `retry` function will always throw an error. | ||
If there's retries left, it will throw a "retry" error that will be handled internally to call `fn`again. | ||
If there's no retries left, it will throw the actual error passed to it. | ||
```js | ||
var promiseRetry = require('promise-retry'); | ||
// Simple example | ||
promiseRetry(function (retry) { | ||
return doSomething() | ||
.then(null, function (err) { | ||
if (err.code === 'ETIMEDOUT') { | ||
// Will throw a retry error that will cause the function | ||
// to be called again or will throw the actual error if there's | ||
// no retries left | ||
throw retry(err) | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
.catch(retry); | ||
}) | ||
@@ -53,3 +52,15 @@ .then(function (value) { | ||
// Conditional example | ||
promiseRetry(function (retry) { | ||
return doSomething() | ||
.catch(function (err) { | ||
if (err.code === 'ETIMEDOUT') { | ||
retry(err); | ||
} | ||
throw err; | ||
}) | ||
}); | ||
## Tests | ||
@@ -56,0 +67,0 @@ |
@@ -16,3 +16,3 @@ 'use strict'; | ||
count += 1; | ||
throw retry(new Error('foo')); | ||
retry(new Error('foo')); | ||
} | ||
@@ -57,3 +57,3 @@ | ||
it('should pass options to the underlying retry module', function (done) { | ||
it.only('should pass options to the underlying retry module', function (done) { | ||
var count = 0; | ||
@@ -66,3 +66,3 @@ | ||
count += 1; | ||
throw retry(new Error('foo')); | ||
retry(new Error('foo')); | ||
} | ||
@@ -87,3 +87,3 @@ | ||
count += 1; | ||
throw retry(new Error('foo')); | ||
retry(new Error('foo')); | ||
} | ||
@@ -123,2 +123,20 @@ | ||
}); | ||
it('should work with several retries in the same chain', function (done) { | ||
promiseRetry(function (retry) { | ||
return Promise.delay(10) | ||
.then(function () { | ||
retry(new Error('foo')); | ||
}) | ||
.catch(function (err) { | ||
retry(err); | ||
}); | ||
}, { retries: 1, factor: 1 }) | ||
.then(function () { | ||
throw new Error('should not succeed'); | ||
}, function (err) { | ||
expect(err.message).to.be('foo'); | ||
}) | ||
.done(done, done); | ||
}); | ||
}); |
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
10676
158
72