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

fix($resource): Check if timeoutDeferred is null inside $cancelRequest #16037

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 @@ -823,7 +823,9 @@ angular.module('ngResource', ['ng']).

function cancelRequest(value) {
promise.catch(noop);
timeoutDeferred.resolve(value);
if (timeoutDeferred !== null) {
timeoutDeferred.resolve(value);
}
}
};

Expand Down
22 changes: 22 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,28 @@ describe('cancelling requests', function() {

expect(creditCard.$cancelRequest).toBe(noop);
});

it('should not break when calling old `$cancelRequest` after the response arrives', function() {
$httpBackend.whenGET('/CreditCard').respond({});

var CreditCard = $resource('/CreditCard', {}, {
get: {
method: 'GET',
cancellable: true
}
});

var creditCard = CreditCard.get();
var cancelRequest = creditCard.$cancelRequest;

creditCard.$promise.then(function(list) {
cancelRequest();
Copy link
Member

Choose a reason for hiding this comment

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

What is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the piece of code mentioned in the Issue #16016 in the codepen to reproduce this issue. I suppose this is not needed since we'll be calling our function using the expect function?

Copy link
Member

Choose a reason for hiding this comment

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

If you call cancelRequest after flushing (as suggested in https://github.com/angular/angular.js/pull/16037/files#r127979323), then this shouldn't be needed (unless I am missing something).

});

$httpBackend.flush();

expect(creditCard.$cancelRequest).not.toThrow();
Copy link
Member

Choose a reason for hiding this comment

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

This wouldn't fail before either. Please change to:

expect(cancelRequest).not.toBe(noop);
expect(cancelRequest).not.toThrow();

Copy link
Contributor Author

@chirag64 chirag64 Jul 25, 2017

Choose a reason for hiding this comment

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

So, I removed lines 2118 - 2121 and added not.toBe(noop) just above 2124, just like you mentioned in your comments. But now the tests are passing before and after the change I made in resource.js.

Copy link
Member

@gkalpak gkalpak Jul 26, 2017

Choose a reason for hiding this comment

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

Have you also changed creditCard.$cancelRequest to cancelRequest?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's exactly what I missed, thanks 👍🏼

});
});

describe('configuring `cancellable` on the provider', function() {
Expand Down