Skip to content

Commit

Permalink
Check both completion conditions when adding ready/failed/do/doError.
Browse files Browse the repository at this point in the history
Adding a ready after there had been an error would try to push it to
the ready slots, which was null because all these arrays are removed
when an error has occurred. The same was true for do, and the inverse
for doErr and failed after error-free completion.

All these places needed to check both this.completed and this.hasFailed.
Better might be to add Future.prototype.settled() to avoid the need to
repeat the checks.
  • Loading branch information
davidmason committed Apr 9, 2015
1 parent c86e44c commit 8f3b8e4
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ function Future() {
Future.prototype.ready = function(slot) {
if(this.completed)
slot(this.value);
else
else if (!this.hasFailed)
this.slots.push(slot);
}

Future.prototype.failed = function(slot) {
if(this.hasFailed)
slot(this.error);
else
else if (!this.completed)
this.failslots.push(slot);
}

Expand Down Expand Up @@ -77,7 +77,7 @@ Future.prototype.complete = function(val) {
}

Future.prototype.fail = function(err) {
var me = this;
var me = this;
if(this.completed || this.hasFailed)
throw "Can't complete an already settled future!"

Expand Down Expand Up @@ -108,7 +108,6 @@ Future.prototype.fmap = function(fn) {
} catch(err) {
fut.fail(err);
}

});
this.failed(function(err) {
fut.fail( err );
Expand Down Expand Up @@ -137,16 +136,14 @@ Future.prototype.flatten = function() {
this.failed(function(err) {
fut.fail(err);
});
this.ready(function(fut2) {
fut2.failed( function(err){
fut.fail(err);
} );
});
this.ready(function(fut2) {
fut2.ready( function(val){
fut.complete(val);
} );
});
fut2.failed( function(err){
fut.fail(err);
} );
});
return fut;
}

Expand Down Expand Up @@ -209,7 +206,7 @@ Future.prototype.do = function(action) {
if(this.completed) {
action(this.value);
fut.complete(this.value);
} else {
} else if (!this.hasFailed) {
this.actions.push(function(v) {
action(v);
fut.complete(v);
Expand All @@ -223,8 +220,8 @@ Future.prototype.doError = function(action) {
if(this.hasFailed) {
action(this.error);
fut.fail(this.error);
} else {
this.actions.push(function(err) {
} else if (!this.completed) {
this.failactions.push(function(err) {
action(err);
fut.fail(err);
});
Expand Down

0 comments on commit 8f3b8e4

Please sign in to comment.