Created
May 3, 2018 23:30
-
-
Save luciopaiva/2ae2e7922123477945ae0502d513b832 to your computer and use it in GitHub Desktop.
Javascript: AJAX helper functions
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
async function getJson(url) { | |
return JSON.parse(await getFile(url)); | |
} | |
async function getFile(url) { | |
return new Promise((resolve, reject) => { | |
const request = new XMLHttpRequest(); | |
request.addEventListener("load", function () { | |
try { | |
resolve(this.responseText); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
request.open("GET", url); | |
request.send(); | |
request.addEventListener("error", reject) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment