Needless Patchouli Manufacture

back-off

2.1.0 • Public • Published

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.

install

npm i back-off

Downloadsweekly downloads

11

version

2.1.0

license

Unlicense

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability