Skip to content

Instantly share code, notes, and snippets.

@zzarcon
Created March 20, 2016 20:28
Show Gist options
  • Select an option

  • Save zzarcon/bb6beb8c7e3fae840482 to your computer and use it in GitHub Desktop.

Select an option

Save zzarcon/bb6beb8c7e3fae840482 to your computer and use it in GitHub Desktop.
xhr promises
function xhrPromise() {
return new Promise(function(resolve, eject) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = onready;
xhr.open("GET", "https://api.github.com/users/zzarcon");
xhr.send();
function onready() {
if (xhr.readyState == 4 && xhr.status == 200) {
var json = JSON.parse(xhr.responseText);
resolve(json);
}
}
});
}
xhrPromise().then(callback);
function callback(user) {
console.log(user.name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment