Implementation of circuit breaker design pattern for nodejs
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test
.babelrc
.gitignore
.nvmrc.test.json
.travis.yml
cb.js
index.js
package-lock.json
package.json
readme.md

readme.md

Back-Off

Circuit breaker design pattern for JavaScript

Build Status

This module let's you use the circuit breaker pattern and call a function multiple times. In addition you can specify a delay to be applied between attempts as well as extending the delay as attempts are made.

Async/Await

import BackOff from 'back-off';
const backoff = new BackOff({
    times: 5, //number of times method should be called
    delay: 50, //delay in milliseconds between calls
    backoff: true // if the delay should be doubled between execution attempts
});
try {
  const result = await backoff.executeAsync(asyncTask);
} catch (error) {
  //do something with the final error
}

Promise

import BackOff from 'back-off';
const backoff = new BackOff({
    times: 5, //number of times method should be called
    delay: 50, //delay in milliseconds between calls
    backoff: true // if the delay should be doubled between execution attempts
});

backoff.executeAsPromise(() => {
  //do something here that may fail
})
.then(()=> {
  // do something else
})
.catch(() => {
  //attempts failed
});

Callback

const BackOff = require('back-off').default;
const backoff = new BackOff({
    times: 5, //number of times method should be called
    delay: 50, //delay in milliseconds between calls
    backoff: true // if the delay should be doubled between execution attempts
});

backoff.execute(() => {
  //do something here
}, (err) => {
  //do something here.
  if(err) {
    it failed
  }
});
// or fire and forget
backoff.execute(() => {
  //do something here
});

The tests show the module in action.