Skip to content

Instantly share code, notes, and snippets.

@hpneo
Created February 11, 2013 03:45
Show Gist options
  • Save hpneo/4752400 to your computer and use it in GitHub Desktop.
Save hpneo/4752400 to your computer and use it in GitHub Desktop.
var Promise = function(url) {
var returned_values = {
complete : null,
fail : null
};
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true, null, null);
this.getData = function() {
return returned_values.complete;
}
this.getError = function() {
return returned_values.fail;
}
this.when = function(callback) {
xhr.onreadystatechange = function() {
if(this.readyState == 4) {
returned_values.complete = JSON.parse(xhr.response);
callback.call(null);
}
}
xhr.onerror = function() {
returned_values.fail = xhr.statusText;
}
xhr.send(null);
return this;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment