Skip to content

Commit

Permalink
.timeout() cancels instead of rejecting the promise
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed May 22, 2014
1 parent 147f22d commit b0dd919
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ module.exports = function( grunt ) {
};

var optionalModuleRequireMap = {
"timers.js": true,
"race.js": true,
"call_get.js": true,
"generators.js": true,
Expand All @@ -97,7 +96,8 @@ module.exports = function( grunt ) {
"using.js": true,
"filter.js": ["map.js"],
"any.js": ["some.js"],
"each.js": ["reduce.js"]
"each.js": ["reduce.js"],
"timers.js": ["cancel.js"]
};

function getOptionalRequireCode( srcs ) {
Expand Down
4 changes: 2 additions & 2 deletions src/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var afterTimeout = function Promise$_afterTimeout(promise, message, ms) {
var err = new TimeoutError(message);
errors.markAsOriginatingFromRejection(err);
promise._attachExtraTrace(err);
promise._rejectUnchecked(err);
promise._cancel(err);
};

var afterDelay = function Promise$_afterDelay(value, promise) {
Expand Down Expand Up @@ -67,7 +67,7 @@ Promise.prototype.timeout = function Promise$timeout(ms, message) {
ret._propagateFrom(this, PROPAGATE_ALL);
ret._follow(this);
_setTimeout(afterTimeout, ms, ret, message, ms);
return ret;
return ret.cancellable();
};

};
14 changes: 14 additions & 0 deletions test/mocha/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ describe("timeout", function () {
done();
});
});

it("should propagate the timeout error to cancellable parents", function(done) {
function doExpensiveOp() {
return new Promise(function() {

})
.cancellable()
.caught(Promise.TimeoutError, function(e) {
done();
})
}

doExpensiveOp().timeout(100);
});
});

describe("delay", function () {
Expand Down

0 comments on commit b0dd919

Please sign in to comment.