Skip to content

Instantly share code, notes, and snippets.

@beardlessman
Created August 8, 2017 05:04
Show Gist options
  • Save beardlessman/8cf45c6f679809ecd20a5f16d0f21dc8 to your computer and use it in GitHub Desktop.
Save beardlessman/8cf45c6f679809ecd20a5f16d0f21dc8 to your computer and use it in GitHub Desktop.
vanilla ajax get (with promise)
function ajaxGet(url) {
return new Promise(function(resolve, reject) {
let req = new XMLHttpRequest();
req.open("GET", url);
req.onload = function() {
if (req.status === 200) {
resolve(req.response);
} else {
reject(new Error(req.statusText));
}
};
req.onerror = function() {
reject(new Error("Network error"));
};
req.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment