Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1904596

Browse files
Ali Millsvojtajina
Ali Mills
authored andcommitted
fix($timeout): allow calling $timeout.cancel() with undefined
This is how it worked in rc9, before refactoring $defer into $timeout.
1 parent 2214338 commit 1904596

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/ng/timeout.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ function $TimeoutProvider() {
7070
* Cancels a task associated with the `promise`. As a result of this the promise will be
7171
* resolved with a rejection.
7272
*
73-
* @param {Promise} promise Promise returned by the `$timeout` function.
73+
* @param {Promise=} promise Promise returned by the `$timeout` function.
7474
* @returns {boolean} Returns `true` if the task hasn't executed yet and was successfully
7575
* canceled.
7676
*/
7777
timeout.cancel = function(promise) {
78-
if (promise.$$timeoutId in deferreds) {
78+
if (promise && promise.$$timeoutId in deferreds) {
7979
deferreds[promise.$$timeoutId].reject('canceled');
8080
return $browser.defer.cancel(promise.$$timeoutId);
8181
}

test/ng/timeoutSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,10 @@ describe('$timeout', function() {
142142
expect($timeout.cancel(promise1)).toBe(false);
143143
expect($timeout.cancel(promise2)).toBe(true);
144144
}));
145+
146+
147+
it('should not throw a runtime exception when given an undefined promise', inject(function($timeout) {
148+
expect($timeout.cancel()).toBe(false);
149+
}));
145150
});
146151
});

0 commit comments

Comments
 (0)