Created
August 8, 2017 05:04
-
-
Save beardlessman/8cf45c6f679809ecd20a5f16d0f21dc8 to your computer and use it in GitHub Desktop.
vanilla ajax get (with 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 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