Skip to content

Instantly share code, notes, and snippets.

@alejandrolechuga
Created February 19, 2019 06:52
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 alejandrolechuga/106e062fba585a18c398d2fbe9622729 to your computer and use it in GitHub Desktop.
Save alejandrolechuga/106e062fba585a18c398d2fbe9622729 to your computer and use it in GitHub Desktop.
// Promesas (Promises) ES6
// XHR request XMLHttpRequest
// cross domain request
// https://www.mocky.io/
function request(url){
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.responseText);
} else {
reject(xhr.responseText);
}
};
xhr.send();
});
}
request('https://www.mocky.io/v2/5c6b866f3200007609bef489')
.then((data) => {
var parsed = JSON.parse(data);
console.log(parsed);
})
.catch(() => {
console.log('failure');
});
request('https://www.mocky.io/v2/5c6b877a320000e40abef490')
.then((data) => {
var parsed = JSON.parse(data);
console.log(parsed);
})
.catch((data) => {
var parsed = JSON.parse(data);
console.log('failure', parsed);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment