Created
September 21, 2016 10:17
-
-
Save kadavre/d85be8254f11593f8c1a4c447b6703f0 to your computer and use it in GitHub Desktop.
ajax jquery promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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