From 971f43d63bc4fb9c4c5f634ed16b7e3ca3c1e589 Mon Sep 17 00:00:00 2001 From: Jonas Pfenniger Date: Wed, 20 Jan 2010 23:21:44 +0100 Subject: [PATCH] FIX: Promise timeout should not addErrback Because now, we expect the exception to be thrown if no errback is given, we can't let timeout() add an errback silently. --- src/node.js | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/node.js b/src/node.js index 3062d859b6..b905f17430 100644 --- a/src/node.js +++ b/src/node.js @@ -202,45 +202,39 @@ var eventsModule = createInternalModule('events', function (exports) { process.Promise = exports.Promise; exports.Promise.prototype.timeout = function(timeout) { - if (timeout === undefined) { + if (!timeout) { return this._timeoutDuration; } - + this._timeoutDuration = timeout; - if (this._timer) { - clearTimeout(this._timer); - this._timer = null; - } - - var promiseComplete = false; - var onComplete = function() { - promiseComplete = true; - if (this._timer) { - clearTimeout(this._timer); - this._timer = null; - } - }; - - this - .addCallback(onComplete) - .addErrback(onComplete); + + if (this.hasFired) return; + this._clearTimeout(); var self = this; this._timer = setTimeout(function() { self._timer = null; - if (promiseComplete) { + if (self.hasFired) { return; } self.emitError(new Error('timeout')); - }, this._timeoutDuration); + }, timeout); return this; }; + + exports.Promise.prototype._clearTimeout = function() { + if (!this._timer) return; + + clearTimeout(this._timer); + this._timer = null; + } exports.Promise.prototype.emitSuccess = function() { if (this.hasFired) return; this.hasFired = true; + this._clearTimeout(); this._values = Array.prototype.slice.call(arguments); this.emit.apply(this, ['success'].concat(this._values)); @@ -249,6 +243,7 @@ var eventsModule = createInternalModule('events', function (exports) { exports.Promise.prototype.emitError = function() { if (this.hasFired) return; this.hasFired = true; + this._clearTimeout(); this._values = Array.prototype.slice.call(arguments); this.emit.apply(this, ['error'].concat(this._values));