Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Created July 18, 2013 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/6032051 to your computer and use it in GitHub Desktop.
Save juandopazo/6032051 to your computer and use it in GitHub Desktop.
DOM Promises are read only
function ajax(url) {
return new Promise(function (resolver) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolver.resolve(xhr.responseText);
} else {
resolver.reject(new Error('There was a problem with the request.'));
}
}
};
xhr.open('GET', url);
xhr.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment