Angularjs promise error deprecated

149 views
Skip to first unread message

mark goldin

unread,
Sep 11, 2015, 12:53:51 PM9/11/15
to AngularJS
Is that true or it's old news?

Thanks

Emmanuel DEMEY

unread,
Sep 11, 2015, 2:31:56 PM9/11/15
to ang...@googlegroups.com

Can you give more precision ? If your talking about the $http().error() function, yes it has been deprecated in angular 1.4.

Le 11 sept. 2015 18:54, "mark goldin" <markz...@gmail.com> a écrit :
Is that true or it's old news?

Thanks

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

mark goldin

unread,
Sep 11, 2015, 2:43:14 PM9/11/15
to ang...@googlegroups.com
Yes, correct. I am talking about that. Thus, the question is: what is a design pattern to catch errors?

You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/pU8sW45XZxo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

Matt Bailey

unread,
Sep 11, 2015, 3:36:21 PM9/11/15
to AngularJS
You can use .catch() for error handling on the promise that $http returns.

mark goldin

unread,
Sep 11, 2015, 3:40:28 PM9/11/15
to AngularJS
But still, the error has to be originated on the server, correct?

--

Matt Bailey

unread,
Sep 11, 2015, 6:11:32 PM9/11/15
to AngularJS
If there is an exception or error in one of the promise handlers (such as in the .then() or possibly even the .error() ), the catch() should fire. If there is a server side error, the catch() should also fire. 

mark goldin

unread,
Sep 11, 2015, 9:16:46 PM9/11/15
to AngularJS
Can you please elaborate on that? 

Sander Elias

unread,
Sep 11, 2015, 11:02:51 PM9/11/15
to AngularJS
Hi Mark,

Straight from the documentation page:
Deprecation Notice
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If$httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

So, if you want to catch error use the `.catch(errHandler)` in stead.
Yes, it's true. I would say, good riddance. it's a bad idea to have an alternative way to handle promises, as it just causes confusion my a lot of developers.

regards
Sander



Emmanuel DEMEY

unread,
Sep 12, 2015, 3:06:20 AM9/12/15
to ang...@googlegroups.com

Hi

Maybe the second method of the then method. Or catch ?

Manu
@EmmanuelDemey

mark goldin

unread,
Sep 12, 2015, 3:30:43 AM9/12/15
to ang...@googlegroups.com
Here is my code:
module.factory('baseService', function ($http, $q) {

    // instantiate initial object
    var baseService = function () {
    };
    baseService.prototype.execute = function (url, webService, method, params) {
        params = typeof params !== "undefined" ? params : {};
        var deferred = $q.defer();
        var response = $http({
            method: "post",
            dataType: "json",
            data: JSON.stringify(params),
            headers: {
                'Content-Type': "application/json; charset=utf-8",
            },
            url: url + 'services/' + webService + "/" + method
        });
        response.success(function (data) {
            deferred.resolve(data);
        });
        response.error(function (data) {
            alert('Error');
        });
        // Return the promise to the controller
        return deferred.promise;
    };
    return baseService;
});
And in Controller:
baseService.prototype.execute("webservice", "method").then("successfunction");
function successfunction(data){}

How would I change my baseService?

Thanks

Sander Elias

unread,
Sep 12, 2015, 5:12:14 AM9/12/15
to AngularJS

Hi Mark,

change .success in .then and .error in .catch. and done.

Regards
Sander

mark goldin

unread,
Sep 12, 2015, 8:36:37 AM9/12/15
to AngularJS
Do you mean this?
 response.success(function (data) {
            deferred.resolve(data);
        });
        response.error(function (data) {
            alert('Error');
        });

to this?
 response.then(function (data) {
            deferred.resolve(data);
        });
        response.catch(function (data) {
            alert('Error');
        });

But I resolve the promise in Controller:
baseService.prototype.execute("webservice", "method").then("successfunction");

Sander Elias

unread,
Sep 12, 2015, 8:41:55 AM9/12/15
to AngularJS
Yes, that's what I mean. Did you even try to run it? It probably will without issue.

mark goldin

unread,
Sep 12, 2015, 8:57:20 AM9/12/15
to AngularJS
I am confused by the fact that I use 'then' in my service and in controller.

Here is my controller:
service.execute("webservice", "method", parameters).then(serviceSuccess);
How would I change my controller?

Sander Elias

unread,
Sep 12, 2015, 9:14:05 AM9/12/15
to AngularJS
Not at all. there is no difference. an $http call just returns a promise. it always did that.

mark goldin

unread,
Sep 12, 2015, 9:22:08 AM9/12/15
to AngularJS
I tried running it like this:
response.then(function (data) {
            deferred.resolve(data);
        });
        response.catch(function (data) {
            alert('Error');
        });
        // Return the promise to the controller
        return deferred.promise;
And controller:
service.execute("webservice", "method", parameters).then(result);

function result(data) {
   // data.d now is accessed as data.data.d
}

So, what's a benefit of changing it at all? Or I am still doing it wrong?

--

Sander Elias

unread,
Sep 12, 2015, 9:41:56 AM9/12/15
to AngularJS

Mark,

The benefit is that you are no longer using depreciated api-calls.
try: deferred.resolve(data.data);

Regards
Sander

mark goldin

unread,
Sep 12, 2015, 10:03:02 AM9/12/15
to AngularJS
Yes, it works that way. Thank you.

--

mark goldin

unread,
Sep 12, 2015, 10:43:16 AM9/12/15
to AngularJS
Can you please explain why after changing my code I was getting data object inside of the returned data?
Reply all
Reply to author
Forward
0 new messages