Created
February 25, 2018 09:56
-
-
Save a1exlism/a209b3eca72cc29d22ad378d1d07d629 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function getURL(url) { | |
| return new Promise(function(resolve, reject) { | |
| let req = new XMLHttpRequest(); | |
| req.open('GET', url, true); | |
| req.onload = function() { | |
| if (req.status === 200) { | |
| resolve(req.responseText); | |
| } else { | |
| reject(new Error(req.statusText)); | |
| } | |
| }; | |
| req.onerror = function() { | |
| reject(new Error(req.statusText)); | |
| }; | |
| req.send(); | |
| }); | |
| } | |
| let URL = 'http://httpbin.org/get'; | |
| getURL(URL).then(function onFulfilled(data) { | |
| /* usual with JSON format */ | |
| console.log(data); | |
| }).catch(function onRejected(error) { | |
| console.error(error); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment