-
-
Notifications
You must be signed in to change notification settings - Fork 926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reject request on XHR timeout #2581
Conversation
Hacks xhrMock to support promises in the handlers so that a timeout event can be fired and appropriately reject the request promise.
xhr.ontimeout = function (ev) { | ||
if (aborted) return; | ||
var error = new Error("Request timeout"); | ||
error.timeout = ev.target.timeout; | ||
reject(error); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you verified the onreadystatechange
callback isn't also executed? If it is, you'll need to set aborted
to true
to keep anything else from firing.
@@ -155,6 +155,13 @@ module.exports = function($window, Promise, oncompletion) { | |||
} | |||
} | |||
|
|||
xhr.ontimeout = function (ev) { | |||
if (aborted) return; | |||
var error = new Error("Request timeout"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's be a little clearer on the error message
var error = new Error("Request timeout"); | |
var error = new Error("Request timed timeout"); |
return new Promise(function (resolve) { | ||
resolve({status: 200, responseText: JSON.stringify({a: 1})}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this.
return new Promise(function (resolve) { | ||
resolve({status: 200, responseText: JSON.stringify({a: 1})}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this.
@zacharyhamm Ping? |
Sorry! I've been distracted by other responsibilities. I'll get this in
a good shape this week :)
|
@zacharyhamm Any chance you could have a look within the upcoming week or so? 🙂 |
I replicated @zacharyhamm 's fix on flems.io here (make sure you enable throttling in your browser). I'm still not getting the proper reject after enabling throttling. Please let me know if I missed something in my example. It seems the issue is that as @isiahmeadows suggests, the Assuming there was an error, and the if (success) resolve(response)
else {
if (xhr.status === 0) {
aborted = true
return
}
try { message = ev.target.responseText }
catch (e) { message = response }
var error = new Error(message)
error.code = ev.target.status
error.response = response
reject(error)
} And then: xhr.ontimeout = function (ev) {
var error = new Error("Request timed out");
error.code = ev.target.status;
reject(error);
} See flems of this |
Created a separate PR #2646 |
Closed in favor of #2646 which has been merged. |
Hacks xhrMock to support promises in the handlers so that a timeout
event can be fired and appropriately reject the request promise.
Description
Adds a handler to XMLHttpRequests 'ontimeout' event so that request will reject client-side timeouts.
Motivation and Context
Fixes #2559
How Has This Been Tested?
Tested with a test in test-request.js and also manual testing
Types of changes
Checklist:
docs/change-log.md