Unable to cancel binding promise returned via $ interval

If I create a promise using $ interval, it is canceled See: http://jsbin.com/jeweke/2/

timer = $interval(intervalFunc, intervalDelay, 10); timer.then( function(res) {console.log('ok', res);}, function(err) {console.log('err', err);} ); 

However, if I tie the promise, the returned promise is not canceled. See: http://jsbin.com/jeweke/1/

 timer = $interval(intervalFunc, intervalDelay, 10) .then( function(res) {console.log('ok', res);}, function(err) {console.log('err', err);} ); 

What gives? Is that exactly how it should work?

Note. The example here is freely adapted from http://jsfiddle.net/ExpertSystem/fZc3W/

+5
source share
1 answer

This is because the result of the promise chain from $interval does not have a property that contains the interval id ( $$intervalId ). In the first case, you keep the promise of the timer that has $intervalId , in the second case, you keep the promise returned from the chain, which is the raw q promise without the $intervalId property (which is a custom property added by promise, keep the corresponding setInterval id when calling $interval(... ). When you cancel the timer, it needs $intervalId to cancel the $intervalId and reject the promise of the timer.

This is what interval.cancel does

  interval.cancel = function(promise) { if (promise && promise.$$intervalId in intervals) { intervals[promise.$$intervalId].reject('canceled'); clearInterval(promise.$$intervalId); delete intervals[promise.$$intervalId]; return true; } return false; }; 

Pay attention to the line: -

  if (promise && promise.$$intervalId in intervals) { 

intervals is nothing more than an intervalId card and its corresponding promise (example: - {1:promiseOfInterval1, 2:promiseOfInterval2} ), so without intervalId it is not canceled. In short, the promise returned by the $ interval is the q promise plus the $ intervalId property, and when you are focused on it, it’s just an implementation of $q that returns the promise of the deferred object.

+6
source

Source: https://habr.com/ru/post/1205234/


All Articles