Skip to content

Instantly share code, notes, and snippets.

@bbaaxx
Last active May 8, 2018 03:09
Show Gist options
  • Save bbaaxx/a7ae8a85b613c07d9ebc215e57d44ec4 to your computer and use it in GitHub Desktop.
Save bbaaxx/a7ae8a85b613c07d9ebc215e57d44ec4 to your computer and use it in GitHub Desktop.
ASY-ES6-01
const crearCartera = saldo => ({
retirar: cantidad =>
new Promise(
(resolve, reject) =>
saldo > cantidad
? resolve((saldo -= cantidad))
: reject(`tienes ${saldo} y no te alcanza :(`),
),
depositar: cantidad => Promise.resolve((saldo += cantidad)),
consultar: () => Promise.resolve(saldo),
});
const comprar = comida => balance =>
console.log(`Yummy ${comida}!, me quedan $${balance} :)`);
const volverACasaConHambre = e => console.log(`No comiste porque ${e}`);
const miCartera = crearCartera(1000);
miCartera
.retirar(200)
.then(comprar('sandwich')) // Yummy sandwich!, me quedan $800 :)
.catch(volverACasaConHambre);
miCartera
.retirar(850)
.then(comprar('caviar'))
.catch(volverACasaConHambre); // No comiste porque tienes 800 y no te alcanza :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment