circuit-breaker-as-promised
Advanced tools
Comparing version 1.0.1 to 1.1.0
27
index.js
@@ -7,4 +7,9 @@ 'use strict' | ||
module.exports = function (func, opts) { | ||
opts = opts || {} | ||
module.exports = function (func, catcher, opts) { | ||
if (catcher && typeof catcher === 'function') { | ||
opts = opts || {} | ||
} else { | ||
opts = opts || catcher || {} | ||
catcher = function (a) { return a } | ||
} | ||
var breaker = new CircuitBreaker(opts) | ||
@@ -18,13 +23,9 @@ | ||
function (success, failed) { | ||
func.apply(that, args) | ||
.then( | ||
function (value) { | ||
success(value) | ||
resolve(value) | ||
}, | ||
function (err) { | ||
failed(err) | ||
reject(err) | ||
} | ||
) | ||
var promise = func.apply(that, args) | ||
promise | ||
.then(resolve, reject) | ||
catcher(promise) | ||
.then(success, failed) | ||
}, | ||
@@ -31,0 +32,0 @@ function () { |
{ | ||
"name": "circuit-breaker-as-promised", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Hysterix-like circuit breaker with promise api", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,1 +31,10 @@ circuit-breaker-as-promised | ||
``` | ||
You often don't want, e.g., http client errors, to trip the breaker, but still | ||
reject the returned promise as in the following example. | ||
```javascript | ||
var breakerRequest = wrapWithBreaker(request, function (res) { | ||
return res.catch(request.error.client) | ||
}) | ||
``` |
49
test.js
@@ -81,2 +81,51 @@ 'use strict' | ||
test('with catcher, should still reject', function (t) { | ||
t.plan(1) | ||
var withBreaker = wrapWithBreaker( | ||
function testFunc () { | ||
return Promise.reject(new RangeError('lol')) | ||
}, | ||
function (result) { | ||
return result.catch(RangeError, function () {}) | ||
} | ||
) | ||
withBreaker() | ||
.then(function () { t.fail('resolved') }) | ||
.catch(RangeError, function () { t.pass('got range error') }) | ||
.catch(function (err) { t.fail(err.stack || err) }) | ||
}) | ||
test('with catcher, after 10 failures, breaker should be closed', function (t) { | ||
t.plan(1) | ||
var withBreaker = wrapWithBreaker( | ||
function testFunc () { | ||
return Promise.reject(new RangeError('lol')) | ||
}, | ||
function (result) { | ||
return result.catch(RangeError, function () {}) | ||
} | ||
) | ||
Promise.map(R.times(R.identity, 10), withBreaker) | ||
.catch(RangeError, R.always(null)) | ||
.then(function () { | ||
return withBreaker() | ||
}) | ||
.then(function () { | ||
t.fail('should not resolve') | ||
}) | ||
.catch(BreakerOpen, function () { | ||
t.fail('breaker is open') | ||
}) | ||
.catch(RangeError, function () { | ||
t.pass('got range error') | ||
}) | ||
.catch(function (err) { | ||
t.fail(err.stack || err) | ||
}) | ||
}) | ||
test('exit', function (t) { | ||
@@ -83,0 +132,0 @@ // HACK because there are timers running to run the circuit breaker checks, |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
6529
150
40
1