0

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;
        };
    }
};
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
ohmyfgod
  • 136
  • 12
  • Why don't you simply use `$http`? – Bergi Jul 14 '15 at 15:00
  • possible duplicate of [How do I promisify native XHR?](http://stackoverflow.com/q/30008114/1048572) – Bergi Jul 14 '15 at 15:03
  • I can't edit offline.js library but I need it so I need another solution. – ohmyfgod Jul 14 '15 at 15:49
  • What do you mean, "you need it"? Apparently you need something different than it. Why can't you simply replace it by an edited version (where, e.g., `Offline.checks.xhr` returns a promise)? – Bergi Jul 14 '15 at 16:00
  • because make hacks in external libraries it bad practice , of course i could make pull request and hope that it could be useful to somebody. – ohmyfgod Jul 14 '15 at 16:07

0 Answers0