From c86c614cacf5385101db54528f5400536bd0a4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Sun, 24 Jan 2010 23:10:25 +0100 Subject: [PATCH] Bug Fix: Late promise promise callbacks firing Late promise bindings would fire regardless of the outcome of the promise. Test case by: Jonas "zimbatm" Pfenniger --- src/node.js | 8 ++++---- test/mjsunit/test-promise.js | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/node.js b/src/node.js index 8cbb7312c8..2ac3b54ca4 100644 --- a/src/node.js +++ b/src/node.js @@ -233,7 +233,7 @@ var eventsModule = createInternalModule('events', function (exports) { exports.Promise.prototype.emitSuccess = function() { if (this.hasFired) return; - this.hasFired = true; + this.hasFired = 'success'; this._clearTimeout(); this._values = Array.prototype.slice.call(arguments); @@ -242,7 +242,7 @@ var eventsModule = createInternalModule('events', function (exports) { exports.Promise.prototype.emitError = function() { if (this.hasFired) return; - this.hasFired = true; + this.hasFired = 'error'; this._clearTimeout(); this._values = Array.prototype.slice.call(arguments); @@ -261,7 +261,7 @@ var eventsModule = createInternalModule('events', function (exports) { }; exports.Promise.prototype.addCallback = function (listener) { - if (this.hasFired) { + if (this.hasFired === 'success') { return listener.apply(this, this._values); } @@ -269,7 +269,7 @@ var eventsModule = createInternalModule('events', function (exports) { }; exports.Promise.prototype.addErrback = function (listener) { - if (this.hasFired) { + if (this.hasFired === 'error') { listener.apply(this, this._values); } diff --git a/test/mjsunit/test-promise.js b/test/mjsunit/test-promise.js index 9b314ea56e..2992f93665 100644 --- a/test/mjsunit/test-promise.js +++ b/test/mjsunit/test-promise.js @@ -19,12 +19,18 @@ a.addCallback(function(value) { assert.equal(TEST_VALUE, value); expectedCallbacks.a1--; }); +a.addErrback(function(error) { + assert.notEqual(TEST_VALUE, error, 'normal'); +}); a.emitSuccess(TEST_VALUE); a.addCallback(function(value) { assert.equal(TEST_VALUE, value); expectedCallbacks.a2--; }); +a.addErrback(function(error) { + assert.notEqual(TEST_VALUE, error, 'late'); +}); // Test regular & late errback binding var b = new Promise();