Skip to content

Instantly share code, notes, and snippets.

@MikeDigitize
Last active March 12, 2017 21:52
Show Gist options
  • Save MikeDigitize/91177e792c9d508d31e6dccaf284b571 to your computer and use it in GitHub Desktop.
Save MikeDigitize/91177e792c9d508d31e6dccaf284b571 to your computer and use it in GitHub Desktop.
/*
var url = '//media.ao.com/cdnbundle/client/4.0.90.0/bundled/BTS/css/MasterStructure.css';
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if(this.readyState === 4 && this.status === 200) {
console.log(this.responseText);
}
};
ajax.open('GET', url, true);
ajax.send();
Convert (only) the above into a new API
Do it in anyway you see fit but:
- Give it a GET method
- Make it Promise based
*/
var link = '//media.ao.com/cdnbundle/client/4.0.90.0/bundled/BTS/css/MasterStructure.css';
var Ajax = (function() {
const ajax = new XMLHttpRequest();
function request(method, url) {
return new Promise(function(res, rej) {
ajax.onreadystatechange = function() {
const { readyState, status, responseText } = this;
if(readyState === 4) {
if(status === 200) {
res(responseText);
}
else {
rej(status);
}
}
};
ajax.open(method, url, true);
ajax.send();
})
}
function get(url) {
return request('GET', url);
}
return { get };
})();
Ajax
.get(link)
.then(function(text) {
console.log('success', text);
})
.catch(function(error) {
console.log('error', error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment