Created
February 19, 2019 06:52
-
-
Save alejandrolechuga/106e062fba585a18c398d2fbe9622729 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
// 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