Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Add guard for aborted requests. #98

Closed
wants to merge 1 commit into from

Conversation

mwistrand
Copy link

Resolves #94. Adds a guard to the onreadystatechange callback so that it only proceeds when the request has not been aborted. As a result, aborted requests will not throw errors in IE9.

The `onreadystatechange` callback should proceed only when the request has not been aborted.
@kitsonk
Copy link
Member

kitsonk commented Dec 14, 2015

Should there be a specific unit test for this, or do you think the existing tests already cover this off fully?

@kitsonk
Copy link
Member

kitsonk commented Dec 14, 2015

Nevermind... I see you comment in #94 about the unit tests.

@@ -60,7 +66,7 @@ export default function xhr<T>(url: string, options: XhrRequestOptions = {}): Re

let timeoutHandle: Handle;
request.onreadystatechange = function (): void {
if (request.readyState === 4) {
if (!isAborted && request.readyState === 4) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we handle (as in reject) isAborted && request.readyState === 4)? Because obviously that is happening at runtime and that should result in a rejected promise, right? With something like resolve(new Error('An aborted request has been resolved'));

Copy link
Author

Choose a reason for hiding this comment

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

If the request times out, then the underlying promise is rejected. However, since the request object itself is unaware of any promise state, it fires that onreadystatechange callback anyway. But you're right; when Task#cancel is executed, the task itself is not rejected. So I wonder if rejecting the promise as aborted within the local abort function and resetting onreadystatechange to a noop function is the way to go here.

Copy link
Member

Choose a reason for hiding this comment

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

While the request object is unaware of the promise state, it is within closure that has the related promise, named as promise, but I agree that the best thing to do is reject and set to a noop.

@kitsonk kitsonk closed this in c36eaa1 Jan 13, 2016
@dylans dylans added this to the Milestone 2 milestone Jan 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants