Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

circuit-breaker-as-promised

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

circuit-breaker-as-promised - npm Package Compare versions

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)
})
```

@@ -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,

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc