0

So i have different states(with different controllers), each state fetching data from api. But if user leave state before request is resolved, then handler is called when he is not in this state. That's not intended. Moreover some of handlers have $intervals fetching data (after resolving 1st request) -> so the data keep fetching and fetching while user moved away hours ago. First thing that came to my mind:

  let isDestroyed = false;

  $http.get('/someurl').then(response => {
    if (isDestroyed) return;

    processResponse();
  });

  $scope.$on('$destroy',() => {
    isDestroyed = true;
  });

What else can i do?

vedmaque
  • 323
  • 1
  • 11

1 Answers1

0

Angular docs:

timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.

Meaning: set timeout to a promise, when destroying the scope - resolve the promise

Endless
  • 34,080
  • 13
  • 108
  • 131