Difference between transformResponse and responseInterceptor?

2,271 views
Skip to first unread message

wingy

unread,
Aug 15, 2012, 2:46:03 AM8/15/12
to ang...@googlegroups.com
It seems that there is two ways to do some logic before the app code is getting the HTTP response:

    $httpProvider.defaults.transformResponse.push(function (data, headersGetter) {
      return data;
    });

and

    $httpProvider.responseInterceptors.push('httpResponseInterceptor');

If I wanna hide a loading image, which one should I use and why?

Wingy

jmeco

unread,
Nov 29, 2012, 11:45:34 AM11/29/12
to ang...@googlegroups.com, johnn...@yobistore.com
Hi Wingy,

Did you find out the difference between these 2 hooks ?

Jean

Jim Hoskins

unread,
Nov 29, 2012, 12:02:59 PM11/29/12
to ang...@googlegroups.com, johnn...@yobistore.com
Looking at the code it seems that transformResponse has to work synchronously. This is because the function receives the data and headers, and must return the data (presumably after transforming it). You can't do anything that requires a callback because you have to return the data, and not a promise.

The responseInterceptors, however, are based on defers/promises. The interceptor itself should return promise.then(....) and since the handlers in the then() call can return either the data to pass along, or a new promise for that data, you are free to perform actions that require callbacks.

I could be wrong, or misreading it. It seems like transformResponse is simpler but lacks the ability to use async code, but response interceptors are more powerful, at the expense of simplicity.

Jean Maynier

unread,
Jan 23, 2013, 1:27:12 PM1/23/13
to ang...@googlegroups.com, johnn...@yobistore.com
As the OP I don't see the differences between the 2. Interceptor is more powerful due to the use of promises (asyn process can be done before returning/sending the data)
When interceptorRequest will be added (https://github.com/angular/angular.js/pull/1701) it will match the transformRequest.

The difference is that you can set transformResponse/transformRequest for all http call or per $http use (and soon per $resource, code is already in trunk). interceptorResponse is only global.

So why not just replace transformResponse/transformRequest by interceptorResponse /interceptorRequest and add it in the config object of $http and $resource ? 

Jean
Reply all
Reply to author
Forward
0 new messages