Browse Source

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 <jonas@pfenniger.name>
v0.7.4-release
Felix Geisendörfer 15 years ago
committed by Ryan Dahl
parent
commit
c86c614cac
  1. 8
      src/node.js
  2. 6
      test/mjsunit/test-promise.js

8
src/node.js

@ -233,7 +233,7 @@ var eventsModule = createInternalModule('events', function (exports) {
exports.Promise.prototype.emitSuccess = function() { exports.Promise.prototype.emitSuccess = function() {
if (this.hasFired) return; if (this.hasFired) return;
this.hasFired = true; this.hasFired = 'success';
this._clearTimeout(); this._clearTimeout();
this._values = Array.prototype.slice.call(arguments); this._values = Array.prototype.slice.call(arguments);
@ -242,7 +242,7 @@ var eventsModule = createInternalModule('events', function (exports) {
exports.Promise.prototype.emitError = function() { exports.Promise.prototype.emitError = function() {
if (this.hasFired) return; if (this.hasFired) return;
this.hasFired = true; this.hasFired = 'error';
this._clearTimeout(); this._clearTimeout();
this._values = Array.prototype.slice.call(arguments); this._values = Array.prototype.slice.call(arguments);
@ -261,7 +261,7 @@ var eventsModule = createInternalModule('events', function (exports) {
}; };
exports.Promise.prototype.addCallback = function (listener) { exports.Promise.prototype.addCallback = function (listener) {
if (this.hasFired) { if (this.hasFired === 'success') {
return listener.apply(this, this._values); return listener.apply(this, this._values);
} }
@ -269,7 +269,7 @@ var eventsModule = createInternalModule('events', function (exports) {
}; };
exports.Promise.prototype.addErrback = function (listener) { exports.Promise.prototype.addErrback = function (listener) {
if (this.hasFired) { if (this.hasFired === 'error') {
listener.apply(this, this._values); listener.apply(this, this._values);
} }

6
test/mjsunit/test-promise.js

@ -19,12 +19,18 @@ a.addCallback(function(value) {
assert.equal(TEST_VALUE, value); assert.equal(TEST_VALUE, value);
expectedCallbacks.a1--; expectedCallbacks.a1--;
}); });
a.addErrback(function(error) {
assert.notEqual(TEST_VALUE, error, 'normal');
});
a.emitSuccess(TEST_VALUE); a.emitSuccess(TEST_VALUE);
a.addCallback(function(value) { a.addCallback(function(value) {
assert.equal(TEST_VALUE, value); assert.equal(TEST_VALUE, value);
expectedCallbacks.a2--; expectedCallbacks.a2--;
}); });
a.addErrback(function(error) {
assert.notEqual(TEST_VALUE, error, 'late');
});
// Test regular & late errback binding // Test regular & late errback binding
var b = new Promise(); var b = new Promise();

Loading…
Cancel
Save