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 Jan 8, 2016
1 parent e6c1449 commit 9f7ed38
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,18 +697,14 @@ 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;
timeoutDeferred = httpConfig.timeout = null;
}
return response;
});

promise = promise.then(
Expand All @@ -717,7 +713,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
3 changes: 1 addition & 2 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 Down

0 comments on commit 9f7ed38

Please sign in to comment.