From 9f7ed380da3491b2a353f67f41c3f92774713b49 Mon Sep 17 00:00:00 2001 From: Lucas Mirelmann Date: Fri, 8 Jan 2016 12:35:38 +0100 Subject: [PATCH] refactor(ngResource): handle success and error callbacks in the same promise resolution --- src/ngResource/resource.js | 14 ++++++++------ test/ngResource/resourceSpec.js | 3 +-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 1080b97e7bea..5dbb909f8cc7 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -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( @@ -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 diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 2d639e245842..6cdc0903a08a 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -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();