Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 589 Bytes

README.md

File metadata and controls

34 lines (28 loc) · 589 Bytes

Switch

A simple circuit breaker library to provide reliability

Example:

create(primaryFunction, secondaryFunction, onErrorTryOther, shouldProbe)

const cb = create(()=>{
  if(Math.random() < 0.5) {
    return Promise.reject("primary err");
  }else {
    return Promise.resolve("primary");
  }
},()=>{
  if(Math.random() < 0.2) {
    return Promise.reject("secondary err");
  }else {
    return Promise.resolve("secondary");
  }
},()=>{
  return true;
},(stats)=>{
  console.log(stats);
  return stats.callsToSecondary > 2;
});

cb([true])
.then((r)=>{
 console.log(r);
});