diff --git a/lib/circuit.js b/lib/circuit.js index d6bb8863..93786e6f 100644 --- a/lib/circuit.js +++ b/lib/circuit.js @@ -315,23 +315,25 @@ class CircuitBreaker extends EventEmitter { return new Promise((resolve, reject) => { const latencyStartTime = Date.now(); if (this.semaphore.test()) { - timeout = setTimeout( - () => { - timeoutError = true; - const error = - new Error(`Timed out after ${this.options.timeout}ms`); - error.code = 'ETIMEDOUT'; - /** - * Emitted when the circuit breaker action takes longer than - * `options.timeout` - * @event CircuitBreaker#timeout - */ - const latency = Date.now() - latencyStartTime; - this.semaphore.release(); - this.emit('timeout', error, latency); - resolve(handleError( - error, this, timeout, args, latency, resolve, reject)); - }, this.options.timeout); + if (this.options.timeout) { + timeout = setTimeout( + () => { + timeoutError = true; + const error = + new Error(`Timed out after ${this.options.timeout}ms`); + error.code = 'ETIMEDOUT'; + /** + * Emitted when the circuit breaker action takes longer than + * `options.timeout` + * @event CircuitBreaker#timeout + */ + const latency = Date.now() - latencyStartTime; + this.semaphore.release(); + this.emit('timeout', error, latency); + resolve(handleError( + error, this, timeout, args, latency, resolve, reject)); + }, this.options.timeout); + } try { const result = this.action.apply(this.action, args);