Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
refactor(ngResource): handle success and error callbacks in the same …
Browse files Browse the repository at this point in the history
…promise resolution
  • Loading branch information
lgalfaso committed Mar 20, 2016
1 parent eaa2d6f commit c9e1eb7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,19 +701,15 @@ angular.module('ngResource', ['ng']).
response.resource = value;

return response;
}, function(response) {
(error || noop)(response);
return $q.reject(response);
});

promise = promise['finally'](function(response) {
promise = promise['finally'](function() {
value.$resolved = true;
if (!isInstanceCall && cancellable) {
value.$cancelRequest = angular.noop;
$timeout.cancel(numericTimeoutPromise);
timeoutDeferred = numericTimeoutPromise = httpConfig.timeout = null;
}
return response;
});

promise = promise.then(
Expand All @@ -722,7 +718,13 @@ angular.module('ngResource', ['ng']).
(success || noop)(value, response.headers);
return value;
},
responseErrorInterceptor);
responseErrorInterceptor || error ?
function(response) {
(error || noop)(response);
(responseErrorInterceptor || noop)(response);
return response;
}
: undefined);

if (!isInstanceCall) {
// we are creating instance / collection
Expand Down
6 changes: 2 additions & 4 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,7 @@ describe("basic usage", function() {
it('should call the error callback if provided on non 2xx response', function() {
$httpBackend.expect('GET', '/CreditCard/123').respond(ERROR_CODE, ERROR_RESPONSE);

var ccs = CreditCard.get({id:123}, callback, errorCB);
ccs.$promise.then(noop, noop);
CreditCard.get({id:123}, callback, errorCB);
$httpBackend.flush();
expect(errorCB).toHaveBeenCalledOnce();
expect(callback).not.toHaveBeenCalled();
Expand All @@ -1063,8 +1062,7 @@ describe("basic usage", function() {
it('should call the error callback if provided on non 2xx response (without data)', function() {
$httpBackend.expect('GET', '/CreditCard').respond(ERROR_CODE, ERROR_RESPONSE);

var ccs = CreditCard.get(callback, errorCB);
ccs.$promise.then(noop, noop);
CreditCard.get(callback, errorCB);
$httpBackend.flush();
expect(errorCB).toHaveBeenCalledOnce();
expect(callback).not.toHaveBeenCalled();
Expand Down

0 comments on commit c9e1eb7

Please sign in to comment.