Created
May 25, 2016 17:09
-
-
Save darrellpratt/408c548cb1ba3b9069dc02ea5b9bc199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function requestOkText(url) { | |
var request = new XMLHttpRequest(); | |
var deferred = Q.defer(); | |
request.open("GET", url, true); | |
request.onload = onload; | |
request.onerror = onerror; | |
request.onprogress = onprogress; | |
request.send(); | |
function onload() { | |
if (request.status === 200) { | |
deferred.resolve(request.responseText); | |
} else { | |
deferred.reject(new Error("Status code was " + request.status)); | |
} | |
} | |
function onerror() { | |
deferred.reject(new Error("Can't XHR " + JSON.stringify(url))); | |
} | |
function onprogress(event) { | |
deferred.notify(event.loaded / event.total); | |
} | |
return deferred.promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment