Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including back-off with all npm packages installed. Try it out:

var backOff = require("back-off")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

back-off v2.0.0

Circuit Breaker pattern in js

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.

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

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.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.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free