Skip to content

Instantly share code, notes, and snippets.

@a1exlism
Created February 25, 2018 09:56
Show Gist options
  • Select an option

  • Save a1exlism/a209b3eca72cc29d22ad378d1d07d629 to your computer and use it in GitHub Desktop.

Select an option

Save a1exlism/a209b3eca72cc29d22ad378d1d07d629 to your computer and use it in GitHub Desktop.
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