-
Notifications
You must be signed in to change notification settings - Fork 1
/
3promises-advanced.js
60 lines (53 loc) · 1.21 KB
/
3promises-advanced.js
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
let resuilt
let creditCheck = function(amount, customer) {
return new Promise((resolve, reject) => {
if (!customer) {
reject(new Error("missing customer!"))
}
if (customer.credit > amount) {
resolve("ok for:"+ amount.toFixed(2))
} else {
resolve("need more credit")
}
})
}
let audit = function(data) {
return new Promise((resolve, reject) => {
if (data.toLowerCase() == "need more credit"){
console.log("AUDIT TRIGGERED!! TRIED TO BUY WITHOUT CREDIT")
}
resolve()
})
}
customer = {
name:"dave",
credit: 40,
}
creditCheck(50,customer).then(function(result){
audit(result)
return (result)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})
creditCheck(50,customer).then(function(result){
// woops!
audit(resuilt)
return (result)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})
// fixed (or at least)
creditCheck(50,customer).then(function(result){
return audit(resuilt)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})