-
Notifications
You must be signed in to change notification settings - Fork 0
/
cliente.ts
76 lines (68 loc) · 2.6 KB
/
cliente.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* eslint-disable no-console */
export class Electrodomestico {
constructor(public descripcion: string, public valor: number) { }
validarCompra(valorMaximo: number): Promise<void> {
return new Promise((resolve, reject) => {
if (valorMaximo < this.valor) {
reject('Mmm... no me convence pagar más de $ ' + valorMaximo + ' por un/a ' + this.descripcion)
}
resolve()
})
}
}
export class Cliente {
saldo = 5000
gastar(concepto: string, valor: number): Promise<void> {
if (this.saldo < valor) {
return Promise.reject('No puedo gastar ' + valor + ' en ' + concepto + '. Tengo $ ' + this.saldo)
}
this.saldo = this.saldo - valor
return Promise.resolve()
}
comprar(cosa: Electrodomestico, valor: number): Promise<void> {
return Promise
.resolve(cosa.validarCompra(valor))
.then(() => this.gastar(cosa.descripcion, cosa.valor))
}
volverEnTaxi(): Promise<void> {
return Promise.resolve(this.gastar('Taxi', 500))
}
procesoDeCompra(cosa: Electrodomestico, valor: number): Promise<void> {
return Promise
.resolve()
// Bien, cada promesa se debe encadenar
.then(() => this.comprar(cosa, valor))
.then(() => this.volverEnTaxi())
// ===============================================
// TODO: que se vea la diferencia entre () => { } y () => expresión, que devuelva la promise
// MAL, al ponerla entre llaves la función es () => void, por lo tanto no devuelve la promesa
// y no se encadena con la siguiente. El error en comprar no se atrapa. El primer test pasa,
// porque las promesas se ejecutan.
// .then(() => { this.comprar(cosa, valor) })
// .then(() => { this.volverEnTaxi() })
// ================================================
// otra cosa que puede pasar
// MAL, no estamos generando la promesa para comprar... por lo tanto no capturamos promesas
// rechazadas, y se rompe. El primer test pasa
// .then(() => {
// this.comprar(cosa, valor)
// return this.volverEnTaxi()
// })
}
}
// const cliente = new Cliente()
// cliente
// OK
// .procesoDeCompra(new Electrodomestico('LCD TV', 4000), 3800)
// No puede volver en Taxi
// .procesoDeCompra(new Electrodomestico('LCD TV', 4700), 4600)
// LCD TV -> no tengo plata
// .procesoDeCompra(new Electrodomestico('LCD TV', 6000), 5100)
// LCD TV -> me la quieren vender muy cara
// .procesoDeCompra(new Electrodomestico('LCD TV', 6000), 6100)
// .then(() => {
// console.log('Proceso de compra finalizado. Saldo: ' + cliente.saldo)
// })
// .catch((e) => {
// console.log(e.message)
// })