Last active
January 19, 2017 23:28
-
-
Save rjpcasalino/a65609d8d22a377ebab0013e372f19b8 to your computer and use it in GitHub Desktop.
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
'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