diff --git a/lib/circuit.js b/lib/circuit.js index 69aded26..969335dd 100644 --- a/lib/circuit.js +++ b/lib/circuit.js @@ -410,8 +410,9 @@ function handleError (error, circuit, timeout, args, latency, resolve, reject) { function fallback (circuit, err, args) { if (circuit[FALLBACK_FUNCTION]) { + const result = circuit[FALLBACK_FUNCTION].apply(circuit[FALLBACK_FUNCTION], args); + if (result instanceof Promise) return result; return new Promise((resolve, reject) => { - const result = circuit[FALLBACK_FUNCTION].apply(circuit[FALLBACK_FUNCTION], args); /** * Emitted when the circuit breaker executes a fallback function * @event CircuitBreaker#fallback diff --git a/test/test.js b/test/test.js index 3c828fbd..87f01903 100644 --- a/test/test.js +++ b/test/test.js @@ -581,14 +581,9 @@ test('CircuitBreaker fallback as a rejected promise', (t) => { const breaker = cb(passFail, options); breaker.fallback(() => Promise.reject(new Error('nope'))); - breaker.on('fallback', (resultPromise) => { - resultPromise - .then(t.fail) - .catch((e) => t.equals('nope', e.message)) - .then(t.end); - }); - - breaker.fire(input).catch(() => {}); + breaker.fire(input).then(t.fail).catch(e => { + t.equals('nope', e.message); + }).then(t.end); }); test('CircuitBreaker fallback as a CircuitBreaker', (t) => {