ajoslin/angular-promise-tracker

Improper tracked[] element removal

Opened this issue · 0 comments

I ran into an issue where active() was returning false even though I had added a promise. The problem was due to the following sequence:

  1. call addPromise(myPromise)
  2. call cancel()
  3. myPromise.reject() // Fails even if you do this on step 2 due to cancel being synchronous and reject not
  4. addPromise(myPromise2)

What happens is onDone is called for myPromise which is not found in the tracked array, so index is -1. The splice operation removes something off the end of the array (which is myPromise2 at this point) when the index is -1

function onDone(isError) {
          return function(value) {
            (minDurationPromise || $q.when()).then(function() {
              var index = tracked.indexOf(deferred);
              tracked.splice(index, 1);

Should be:

 function onDone(isError) {
          return function(value) {
            (minDurationPromise || $q.when()).then(function() {
              var index = tracked.indexOf(deferred);
              if (index != -1) {
                  tracked.splice(index, 1);
              }