Skip to content

Instantly share code, notes, and snippets.

@rjpcasalino
Last active January 19, 2017 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjpcasalino/a65609d8d22a377ebab0013e372f19b8 to your computer and use it in GitHub Desktop.
Save rjpcasalino/a65609d8d22a377ebab0013e372f19b8 to your computer and use it in GitHub Desktop.
'use strict';
let getUrl= (url) => {
// return a new Promise.
return new Promise((resolve, reject) => {
let req = new XMLHttpRequest();
req.open('GET', url);
// this is where the magic happens.
req.onload = () => {
if (req.status == 200) {
resolve(req);
} else {
reject(req.status);
}
};
// Handle network error
req.onerror = () => {
reject(Error('Network is shitty. Whoops. Fuck.'));
};
// Makes the request
req.send();
});
};
getUrl('https://api.github.com/users/Gusbenz').then((res) => {
console.log('Success!', res.status);
})
.catch((res) => {
console.error('Error', res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment