Last active
February 11, 2020 22:50
-
-
Save yutakatsuchida/b3d2140d8231ed617f6268b276bbdb0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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