I'm using angular and have a problem with wrapping XMLHttpRequest for $q.
I'm also using offline.js library to make check if user online or not. It change state of var 'status' to down\up after making an ajax request to fav.ico. It make ajax call after calling Offline.check() .
Offline.check() return XMLHttpRequest that I want to wrap to $q and make promise to use result after getting it
I tried :
$q( Offline.check() ).then(function(response){
console.log(response);
});
but get Error: $q:norslvr No resolver function passed to $Q
interesting part of offline.js
Offline.check = function() {
Offline.trigger('checking');
return Offline.checks['xhr']();
};
Offline.checks.xhr = function() {
var e, xhr;
xhr = new XMLHttpRequest;
xhr.offline = false;
xhr.open('HEAD', Offline.getOption('checks.xhr.url'), true);
if (xhr.timeout != null) {
xhr.timeout = Offline.getOption('checks.xhr.timeout');
}
checkXHR(xhr, Offline.markUp, Offline.markDown);
try {
xhr.send();
} catch (_error) {
e = _error;
Offline.markDown();
}
return xhr;
};
checkXHR = function(xhr, onUp, onDown) {
var checkStatus, _onerror, _onload, _onreadystatechange, _ontimeout;
checkStatus = function() {
if (xhr.status && xhr.status < 12000) {
return onUp();
} else {
return onDown();
}
};
if (xhr.onprogress === null) {
_onerror = xhr.onerror;
xhr.onerror = function() {
onDown();
return typeof _onerror === "function" ? _onerror.apply(null, arguments) : void 0;
};
_ontimeout = xhr.ontimeout;
xhr.ontimeout = function() {
onDown();
return typeof _ontimeout === "function" ? _ontimeout.apply(null, arguments) : void 0;
};
_onload = xhr.onload;
return xhr.onload = function() {
checkStatus();
return typeof _onload === "function" ? _onload.apply(null, arguments) : void 0;
};
} else {
_onreadystatechange = xhr.onreadystatechange;
return xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
checkStatus();
} else if (xhr.readyState === 0) {
onDown();
}
return typeof _onreadystatechange === "function" ? _onreadystatechange.apply(null, arguments) : void 0;
};
}
};