angular $http / jquery complete equivalent
Asked Answered
M

1

13

Is there a way to emulate jquery 'complete' callback with angular $http module? I have some code I would like to execute no matter whether the request succeeded or failed and at the moment I find myself having to write this:

$http.get(someUrl).success(function(){
        successCode();
        completeCode();
    }).error(function(){
        errorCode();
        completeCode();
    })

but I would rather write something like:

$http.get(someUrl).success(function(){
        successCode();
    }).error(function(){
        errorCode();
    }).complete(function(){
        completeCode();
    })

I've tried also using the promise API but I end up having the same problem. Any suggestion?

Marleen answered 9/8, 2013 at 10:2 Comment(0)
M
14

Update Aug 2014: .always has been renamed .finally in recent versions of Angular. Prefer .finally to .always.

Note that in order to support IE8 you have to call it with bracket notation as ["finally"].


You can use .always in AngularJS

This change is rather new (you could do that in jQuery for a while), you can see the commit here. This requires you to have AngularJS 1.1.5 or higher.

always(callback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.

Fiddle

Magic answered 9/8, 2013 at 10:8 Comment(9)
You should mention it is in versions 1.1.5 and later.Scalp
@Scalp Right, I've said it's rather new and added a link to the commit message, but I guess saying the version number is also a good idea - this is what I get for living on the repo :)Magic
@Reno Please check which version of Angular you're using and the fiddle I added to the answer.Magic
@BenjaminGruenbaum my bad I was using 1.0.7, it works like a charm indeed :)Marleen
Tried the always callback, but for some reason the code inside is fired before the request is actually completed. So had to stick with promise.then :(Mcferren
It doesn't seem to work with the current angular v1.2.15. Looks like always doesn't exist anymore, I'm getting an error. Any way to fix this?Christenson
Note: The angular documentation includes the following proviso, "Because finally is a reserved word in JavaScript and reserved keywords are not supported as property names by ES3, you'll need to invoke the method like promise['finally'](callback) to make your code IE8 and Android 2.x compatible."Saiff
I've plagiarized your answer on another question (Angular equivalent of .done in jquery). I hope that's OK.Vermicide
@Vermicide it's not plagiarism since you provided attribution - plagiarism is when you claim the content as your own. It is explicitly allowed to quote and use other answers with attribution in SO - that's the content license (scroll to the very bottom of every page - it's always there). So to sum it up - yes, what you did is perfectly fine and you don't have to ask for permission or notify me in the future.Magic

© 2022 - 2024 — McMap. All rights reserved.