Skip to content

Instantly share code, notes, and snippets.

@moisesnandres
Created April 25, 2018 15:13
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 moisesnandres/37541c10856de96e05d5fd1c6e32a3a0 to your computer and use it in GitHub Desktop.
Save moisesnandres/37541c10856de96e05d5fd1c6e32a3a0 to your computer and use it in GitHub Desktop.
IcaJS
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