Created
April 25, 2018 15:13
-
-
Save moisesnandres/37541c10856de96e05d5fd1c6e32a3a0 to your computer and use it in GitHub Desktop.
IcaJS
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 mostrar = false | |
const myPromise = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
if (mostrar) { | |
resolve('hello world!'); | |
} else { | |
reject(Error('hello there!')); | |
} | |
}, 3000) | |
}); | |
myPromise | |
.then(response => { console.log(response) }) | |
.catch(error => { console.log(error) }) | |
const charlas = [ | |
{ id: 1, tema: 'ES6'}, | |
{ id: 2, tema: 'AWS Lambda con NodeJS' }, | |
{ id: 3, tema: 'Promises' }, | |
{ id: 4, tema: 'Firebase basics' } | |
] | |
function getCharla(id) { | |
return new Promise((resolve, reject) => { | |
const charla = charlas.find(charla => charla.id === id) | |
setTimeout(() => { | |
if (charla) { | |
resolve(charla) | |
} else { | |
reject(`Charla ${id} no encontrada`) | |
} | |
}, 2000) | |
}) | |
} | |
getCharla(10) | |
.then(response => {console.log(response) }) | |
.catch(error => { console.error(error.toUpperCase()) }) | |
// SWAPI | |
const people = fetch('https://swapi.co/api/people') | |
const people2 = fetch('https://swapi.co/api/people/?page=2') | |
Promise | |
.all([people, people2]) | |
.then(responses => Promise.all(responses.map(res => res.json()))) | |
.then(responses => { | |
results = responses.map(({ results }) => results) | |
allPeople = [].concat(...results) | |
console.log(allPeople) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment