I am wrapping some of the functionality of XMLHttpRequest
. I am attaching the resolution of a deferrred to the event fired onload
. IIUC XMLHttpRequest
sets the value of this
in the callback invoked by XMLHttpRequest
to contain the response details (response text, status code etc.).
But I am using q
and the value of this
is lost somewhere in the resolution of the deferred. How can I ensure the response details are propagated to the callback resgistered with the promise then
?
XMLHttpRequestWrapper.prototype.get = function(url) {
var deferred = q.defer();
var request = new XMLHttpRequest();
request.onload = function() {
// this now contains the response info
deferred.resolve.apply(this, arguments); // 'this' is lost in the internals of q :(
};
request.onerror = function() {
deferred.reject.apply(this, arguments);
};
request.open('GET', url, true);
request.send();
return deferred.promise;
}