This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix($timeout): throw when trying to cancel non-$timeout promise
Previously, calling `$timeout.cancel()` with a promise that was not generated by a call to `$timeout()` would do nothing. This could, for example, happen when calling `.then()`/`.catch()` on the returned promise, which creates a new promise, and passing that to `$timeout.cancel()`. With this commit, `$timeout.cancel()` will throw an error if called with a non-$timeout promise, thus surfacing errors that would otherwise go unnoticed. Fixes #16424 BREAKING CHNAGE: `$timeout.cancel()` will throw an error if called with a promise that was not generated by `$timeout()`. Previously, it would silently do nothing. Before: ```js var promise = $timeout(doSomething, 1000).then(doSomethingElse); $timeout.cancel(promise); // No error; timeout NOT canceled. ``` After: ```js var promise = $timeout(doSomething, 1000).then(doSomethingElse); $timeout.cancel(promise); // Throws error. ``` Correct usage: ```js var promise = $timeout(doSomething, 1000); var newPromise = promise.then(doSomethingElse); $timeout.cancel(promise); // Timeout canceled. ```
- Loading branch information
Showing
3 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@ngdoc error | ||
@name $timeout:badprom | ||
@fullName Non-$timeout promise | ||
@description | ||
|
||
This error occurs when calling {@link ng.$timeout#cancel $timeout.cancel()} with a promise that | ||
was not generated by the {@link ng.$timeout $timeout} service. This can, for example, happen when | ||
calling {@link ng.$q#the-promise-api then()/catch()} on the returned promise, which creates a new | ||
promise, and pass that new promise to {@link ng.$timeout#cancel $timeout.cancel()}. | ||
|
||
Example of incorrect usage that leads to this error: | ||
|
||
```js | ||
var promise = $timeout(doSomething, 1000).then(doSomethingElse); | ||
$timeout.cancel(promise); | ||
``` | ||
|
||
To fix the example above, keep a reference to the promise returned by | ||
{@link ng.$timeout $timeout()} and pass that to {@link ng.$timeout#cancel $timeout.cancel()}: | ||
|
||
```js | ||
var promise = $timeout(doSomething, 1000); | ||
var newPromise = promise.then(doSomethingElse); | ||
$timeout.cancel(promise); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters