Skip to content

Adds CircuitBreaker#call() method

Compare
Choose a tag to compare
@lance lance released this 16 Oct 13:43
v4.1.0
57df828

This allows the user to control the this context within the function execution for a circuit. This method behaves in many ways like Function.prototype.call(). It takes a this context as the first parameter and any optional parameters as an argument list to the function. Here is an example usage:

const context = {
  lunch: 'sushi'
};

async function getLunch (param) {
  return param
    ? Promise.resolve(param)
    : Promise.resolve(this.lunch);
}
getLunch.lunch = 'tacos';

const circuit = new CircuitBreaker(getLunch);
circuit.call()
  .then(console.log) // logs 'tacos'
  .then(_ => {
    circuit.call(context)
      .then(console.log) // logs 'sushi'
      .then(_ => {
        circuit.call(context, 'burgers')
          .then(console.log); // logs 'burgers'
      });
  });