Skip to content

Instantly share code, notes, and snippets.

@kadavre
Created September 21, 2016 10:17
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 kadavre/d85be8254f11593f8c1a4c447b6703f0 to your computer and use it in GitHub Desktop.
Save kadavre/d85be8254f11593f8c1a4c447b6703f0 to your computer and use it in GitHub Desktop.
ajax jquery promise
function getAjaxPromise (url) {
return new Promise(function (resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.open('GET', url, true);
xhttp.onload = function () {
if (xhttp.status == 200) {
resolve(JSON.parse(xhttp.response));
} else {
reject(xhttp.statusText);
}
};
xhttp.onerror = function () {
reject(xhttp.statusText);
};
xhttp.send();
});
}
var promise = get("data/tweets.json");
promise.then(function(tweets) {
console.log(tweets);
}).catch(function (error) {
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment