Last active
May 8, 2018 03:09
-
-
Save bbaaxx/a7ae8a85b613c07d9ebc215e57d44ec4 to your computer and use it in GitHub Desktop.
ASY-ES6-01
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 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