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

Commit

Permalink
fix($timeout): allow calling $timeout.cancel() with undefined
Browse files Browse the repository at this point in the history
This is how it worked in rc9, before refactoring $defer into $timeout.
  • Loading branch information
Ali Mills authored and vojtajina committed Jun 5, 2012
1 parent 2214338 commit 1904596
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ng/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions test/ng/timeoutSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
});
});

0 comments on commit 1904596

Please sign in to comment.