From b7a4bf9f2abb7698b7abe65db7ee52334e175335 Mon Sep 17 00:00:00 2001 From: Petka Antonov Date: Sun, 24 Jan 2016 00:01:46 +0200 Subject: [PATCH] Fixes #926 --- src/timers.js | 8 ++++++++ test/mocha/cancel.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/timers.js b/src/timers.js index 3f87332a3..8c5158d8a 100644 --- a/src/timers.js +++ b/src/timers.js @@ -56,6 +56,7 @@ function failureClear(reason) { throw reason; } + Promise.prototype.timeout = function (ms, message) { ms = +ms; var parent = this.then(); @@ -63,6 +64,13 @@ Promise.prototype.timeout = function (ms, message) { 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); }; diff --git a/test/mocha/cancel.js b/test/mocha/cancel.js index 733f03c1d..03fb04819 100644 --- a/test/mocha/cancel.js +++ b/test/mocha/cancel.js @@ -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); + }); + }); + }); }