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

Issue with canceling XHR request using promises - fixed #12525 #12657

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,10 @@ angular.module('ngResource', ['ng']).
undefined;

forEach(action, function(value, key) {
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
if (key != 'params' && key != 'isArray' && key != 'interceptor' && key != 'timeout') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just made this a switch statement for readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp Done

httpConfig[key] = copy(value);
} else if (key == 'timeout') {
httpConfig[key] = value;
}
});

Expand Down
34 changes: 33 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ describe("resource", function() {
});

describe('resource', function() {
var $httpBackend, $resource;
var $httpBackend, $resource, $q;

beforeEach(module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
Expand All @@ -1319,6 +1319,7 @@ describe('resource', function() {
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
$resource = $injector.get('$resource');
$q = $injector.get('$q');
}));


Expand Down Expand Up @@ -1356,5 +1357,36 @@ describe('resource', function() {
);
});

it('If timeout promise is resolved, cancel the request', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you change the test description to match the grammar of other ones in the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp done

var canceler = $q.defer();

$httpBackend.when('GET', '/CreditCard').respond({data: '123'});

var CreditCard = $resource('/CreditCard', {}, {
query: {
method: 'GET',
timeout: canceler.promise
}
});

CreditCard.query();

canceler.resolve();
expect(function() { $httpBackend.flush();}).toThrow(new Error("No pending request to flush !"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: expect($httpBackend.flush).toThrow(...); is more concise and readable (here and below).
(Else you need a space after flush();.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it (flush) uses this at all, it's not an option anyways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkalpak fixed


canceler = $q.defer();
CreditCard = $resource('/CreditCard', {}, {
query: {
method: 'GET',
timeout: canceler.promise
}
});

CreditCard.query();
expect(function() { $httpBackend.flush();}).not.toThrow(new Error("No pending request to flush !"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave it at .not.toThrow() (to account for the unlikely situation where the error message changes and the behaviour breaks (simultaneously)).
It's also more concise and readable.



});


});