Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there such a thing as a finally with AngularJS $http?

Tags:

angularjs

I have code like this:

    $http({
        method: 'POST',
        url: "/api/Account/Register",
        data: {
            userName: userName,
            password: password,
            confirmPassword: confirmPassword
        }
    })
    .success(function () {
        successCallback();
    })
    .error(function (data) {
        errorCallback(data);
    })

Is there any way that I could add in a finallyCallback to this with AngularJS ?

like image 833
Samantha J T Star Avatar asked Oct 22 '25 00:10

Samantha J T Star


2 Answers

Yes, there is a finally method since 1.2.0rc1, as shown by the documentation. The method was known as always since 1.1.5.

$http({
        method: 'POST',
        url: "/api/Account/Register",
        data: {
            userName: userName,
            password: password,
            confirmPassword: confirmPassword
        }
    })
    .success(successCallback)
    .error(errorCallback)
    .finally(finallyCallback)
;
like image 175
Blackhole Avatar answered Oct 25 '25 09:10

Blackhole


The promise returned by $http is the same as any other promise created with the $q service. That means it will have a finally method:

$http({ /* ... */ })
.success(function () {})
.error(function () {})
.finally(function () {});

From the $http docs:

Returns a promise object with the standard then method and two http specific methods

like image 33
James Allardice Avatar answered Oct 25 '25 07:10

James Allardice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!