Skip to content

Instantly share code, notes, and snippets.

@luciopaiva
Created May 3, 2018
Embed
What would you like to do?
Javascript: AJAX helper functions
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