Skip to content

Instantly share code, notes, and snippets.

@unjust
Last active August 5, 2021 23:48
Show Gist options
  • Save unjust/720d71641f781b937940c017ee12619d to your computer and use it in GitHub Desktop.
Save unjust/720d71641f781b937940c017ee12619d to your computer and use it in GitHub Desktop.
const fetch = require("node-fetch");
// Una Promesa es un objeto en Javascript que vincula codigo
// que produce un resultado (y puede demorar)
// con codigo que quiere consumir el resultado
// ejemplo de pidiendo comida
// '🍔' '🍟' '🍪'
// * fetch
// * Promise then catch
// * new Promise (setTimeout, callbacks)
// * Promise all
fetch('https://api.discogs.com/releases/249504').then((response) => {
console.log(`Response status ${response.status}`);
// return response.json(); //este va a volver nuevo promise, podemos agregar otro then cuando el callback anterior vuelva un promise
}).catch((err) => {
console.log(`Un error occured ${err}`)
});
const pidoHamburguesas = (cantidad) => {
return new Promise((resolve, reject) => {
const limite = 10;
if (cantidad > limite) {
reject("Menos por favor");
} else {
setTimeout(() => {
const arrHamb = [];
for (let i = 0; i < cantidad; i++) {
arrHamb.push('🍔');
}
resolve(arrHamb);
return arrHamb;
}, 1000 * cantidad);
}
})
}
pidoHamburguesas(11).then((miPedido) => {
console.log("listo", miPedido)
}).catch((mensajeError) => {
console.log(mensajeError);
}).finally(() => console.log("Ya terminado!"));
const f1 = () => fetch('https://api.discogs.com/releases/249504');
const f2 = () => fetch('https://api.discogs.com/artists/1/releases');
const f3 = () => fetch('https://api.discogs.com/artists/100/releases');
Promise.all([ f1(), f2(), f3()]).then((allResp) => {
console.log(allResp);
});
//'https://api.discogs.com/releases/249504'
// https://api.discogs.com/artists/1/releases
// https://api.discogs.com/artists/100/releases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment