Created
January 29, 2019 07:30
-
-
Save 0GiS0/c1e803bfd4ab4ae3dcefa16efd32f4ff 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
const ChuckNorrisFact = () => { | |
return new Promise((resolves, rejects) => { | |
const api = 'https://api.chucknorris.io/jokes/random'; | |
const request = new XMLHttpRequest(); | |
request.open('GET', api); | |
request.onload = () => { | |
if (request.status === 200) { | |
resolves(JSON.parse(request.response)); | |
} else { | |
rejects(new Error(request.response)); | |
} | |
}; | |
request.onerror = err => rejects(err); | |
request.send(); | |
}); | |
}; | |
ChuckNorrisFact().then(fact => console.log(fact), err => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment