Angular JS cancel $http call before calling a new $http
Asked Answered
S

1

5

In angular JS 1.1.5 you can cancel previously started $http calls. These two link1 and link2 gives some explanation on how it works.

However I can't understand, how to use this in practice. I created plnkr to illustrate what I want to achieve.

  1. User has a link he can click on (as we know, users tend to do that very passionately)
  2. If user clicks link, and previous request hasn't yet finished, it should be canceled, and new request should be made (in this example there are other ways to ignore multiple clicks. Original problem is user input field with ng-change on it - request gets made every time user changes value - every keypress will fire $http. I'm using link to make it a bit simpler)

As you can see in plnkr request fires and gets canceled immediately, but I'm expecting last request to succeed.

I'm new to angularjs so probably I'm doing it wrong. I've gooled everything I can imagine, but there are no full examples for canceler.resolve()

Can anyone help me with this or guide me to right direction?

Stealthy answered 1/7, 2013 at 12:31 Comment(0)
D
10

You need to create a new canceler for each request, because once it's resolved it can't be used again.

See here for an updated plnkr.

Doggone answered 11/7, 2013 at 16:23 Comment(1)
Bingo! I was reusing mine. That's why it wasn't working.Tautology

© 2022 - 2024 — McMap. All rights reserved.