Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yutakatsuchida/b3d2140d8231ed617f6268b276bbdb0c to your computer and use it in GitHub Desktop.
Save yutakatsuchida/b3d2140d8231ed617f6268b276bbdb0c to your computer and use it in GitHub Desktop.
function promiseFunction(url) {
return new Promise( function(resolve, reject){
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(xhr.responseText);
} else {
reject(xhr.statusText);
}
}
}
xhr.onerror = function (e) {
reject(xhr.statusText);
};
xhr.send(null);
}).then(function(value){
console.log(value);
}).catch(function(value){
console.log(value);
});
}
promiseFunction('data.json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment