fix($timeout): allow calling $timeout.cancel() with undefined

This is how it worked in rc9, before refactoring $defer into $timeout.
This commit is contained in:
Ali Mills 2012-06-01 14:50:01 -07:00 committed by Vojta Jina
parent 22143381d8
commit 1904596e0c
2 changed files with 7 additions and 2 deletions

View file

@ -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);
}

View file

@ -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);
}));
});
});