diff --git a/src/ng/timeout.js b/src/ng/timeout.js index ac92bf8c98a..5b51f85efae 100644 --- a/src/ng/timeout.js +++ b/src/ng/timeout.js @@ -70,12 +70,12 @@ function $TimeoutProvider() { * Cancels a task associated with the `promise`. As a result of this the promise will be * resolved with a rejection. * - * @param {Promise} promise Promise returned by the `$timeout` function. + * @param {Promise=} promise Promise returned by the `$timeout` function. * @returns {boolean} Returns `true` if the task hasn't executed yet and was successfully * canceled. */ timeout.cancel = function(promise) { - if (promise.$$timeoutId in deferreds) { + if (promise && promise.$$timeoutId in deferreds) { deferreds[promise.$$timeoutId].reject('canceled'); return $browser.defer.cancel(promise.$$timeoutId); } diff --git a/test/ng/timeoutSpec.js b/test/ng/timeoutSpec.js index 19db1227d30..5ee99d98dc1 100644 --- a/test/ng/timeoutSpec.js +++ b/test/ng/timeoutSpec.js @@ -142,5 +142,10 @@ describe('$timeout', function() { expect($timeout.cancel(promise1)).toBe(false); expect($timeout.cancel(promise2)).toBe(true); })); + + + it('should not throw a runtime exception when given an undefined promise', inject(function($timeout) { + expect($timeout.cancel()).toBe(false); + })); }); });