Skip to content

Commit

Permalink
Fixes #926
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed Jan 23, 2016
1 parent c66c2ca commit b7a4bf9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,21 @@ function failureClear(reason) {
throw reason;
}


Promise.prototype.timeout = function (ms, message) {
ms = +ms;
var parent = this.then();
var ret = parent.then();
var handle = setTimeout(function timeoutTimeout() {
afterTimeout(ret, message, parent);
}, ms);
if (debug.cancellation()) {
ret._setOnCancel({
_resultCancelled: function() {
clearTimeout(handle);
}
});
}
return ret._then(successClear, failureClear, undefined, handle, undefined);
};

Expand Down
41 changes: 41 additions & 0 deletions test/mocha/cancel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2941,4 +2941,45 @@ if (testUtils.isNodeJS) {
});
});
});

describe("GH926", function() {
var clear, set;
var clears = 0;
before(function() {
clears = 0;
set = setTimeout;
clear = clearTimeout;
setTimeout = function() {
return set.apply(this, arguments);
};
clearTimeout = function() {
clears++;
return clear.apply(this, arguments);
};
});

after(function() {
clears = 0;
setTimeout = set;
clearTimeout = clear;
});

specify("GH926", function() {
var calls = 0;
var p = new Promise(function(resolve, reject, onCancel) {
onCancel(function() { calls++; });
})
.timeout(10000000)
.lastly(function() {
calls++;
});

p.cancel();

return awaitLateQueue(function() {
assert.equal(2, calls);
assert.equal(1, clears);
});
});
});
}

0 comments on commit b7a4bf9

Please sign in to comment.