-
Notifications
You must be signed in to change notification settings - Fork 1
/
2promises.js
63 lines (53 loc) · 1.18 KB
/
2promises.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 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 customer
customer = {
name:"dave",
credit: 90,
}
creditCheck(50,customer).then(function(result){
console.log(result)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})
customer = {
name:"dave",
credit: 40,
}
creditCheck(50,customer).then(function(result){
console.log(result)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})
creditCheck(50,null).then(function(result){
console.log(result)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})
creditCheck(null,customer).then(function(result){
console.log(result)
}).catch(function(err){
console.log("caught the error:")
console.log("---")
console.log(err.stack)
console.log("---")
})