Skip to content

Instantly share code, notes, and snippets.

@Luftare
Last active October 25, 2017 18:19
Show Gist options
  • Save Luftare/8d6be9034530d4522f99e22ead8085b5 to your computer and use it in GitHub Desktop.
Save Luftare/8d6be9034530d4522f99e22ead8085b5 to your computer and use it in GitHub Desktop.
/*
Minimal AJAX get request method to fetch data. Example usage:
get('example.com/api/products/123')
.then(product => console.log(product.name))
*/
function get(url) {
return new Promise(res => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = () => {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) res(JSON.parse(xmlHttp.responseText));
}
xmlHttp.open('GET', url, true);
xmlHttp.send();
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment