-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_many_try.js
127 lines (109 loc) · 3.54 KB
/
promise_many_try.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
let CalculateTotal = () => {
return new Promise((resolve, reject) => {
let retryNum = 3;
let i = 0;
let totalCalculated = new Array(retryNum);
do {
totalCalculated[i] = Math.random() < 0.5;
console.log("calculateTotal is " + totalCalculated[i], i+1);
if(totalCalculated[i] == true){
resolve(" 'total calculated' ");
console.log("total calculated");
{break;}}
else {i++;}
} while (i<retryNum)
if (totalCalculated[i] != true && i == retryNum) {
reject(" 'calculateTotal failed' ");
console.log("calculateTotal failed");
}
})
}
let UpdateCouponUsage = (message) => {
return new Promise((resolve, reject) => {
let retryNum = 3;
let i = 0;
let couponUsageUpdated = new Array(retryNum);
do {
couponUsageUpdated[i] = Math.random() < 0.5;
console.log("couponUsageUpdate is " + couponUsageUpdated[i], i+1);
if(couponUsageUpdated[i] == true){
resolve(message+" 'coupon updated' ");
console.log("coupon updated");
{break;}}
else {i++;}
} while (i<retryNum)
if (couponUsageUpdated[i] != true && i == retryNum) {
reject(message+" 'couponUpdate failed' ");
console.log("couponUpdate failed");
rollbackCalculateTotal();
}
})
}
let ChargeVendorProcessingFee = (message) => {
return new Promise((resolve, reject) => {
let retryNum = 3;
let i = 0;
let vendorProcessingFeeCharged = new Array(retryNum);
do {
vendorProcessingFeeCharged[i] = Math.random() < 0.5;
console.log("vendorProcessingFeeCharge is " + vendorProcessingFeeCharged[i], i+1);
if(vendorProcessingFeeCharged[i] == true){
resolve(message + " 'vendorProcessingFee charged' ");
console.log("vendorProcessingFee charged");
{break;}}
else {i++;}
} while (i<retryNum)
if (vendorProcessingFeeCharged[i] != true && i == retryNum) {
reject(message + " 'vendorProcessingFeeCharge failed' ");
console.log("vendorProcessingFeeCharge failed");
rollbackUpdateCouponUsage();
rollbackCalculateTotal();
}
})
}
let ChargeCreditCard = (message) => {
return new Promise((resolve, reject) => {
let retryNum = 3;
let i = 0;
let creditCardCharged = new Array(retryNum);
do {
creditCardCharged[i] = Math.random() < 0.5;
console.log("creditCardCharge is "+ creditCardCharged[i], i+1);
if(creditCardCharged[i] == true){
resolve(message + " 'creditCard charged' ");
console.log("creditCard charged");
{break;}}
else {i++;}
} while (i<retryNum)
if (creditCardCharged[i] != true && i == retryNum) {
reject(message + " 'creditCardCharge failed' ");
console.log("creditCardCharge failed");
rollbackChargeVendorProcessingFee();
rollbackUpdateCouponUsage();
rollbackCalculateTotal();
}
})
}
function placeOrder(){
CalculateTotal().then((result) => {
return UpdateCouponUsage(result);
}).then ((result) => {
return ChargeVendorProcessingFee(result);
}).then((result) => {
return ChargeCreditCard(result);
}).then((result) => {
console.log("Summary of Transaction: " + result + "'place-order has succeeded'");
}).catch(((result) => {
console.log("Summary of Transaction: " + result +"'place-order has failed'" );
}))
}
placeOrder();
function rollbackCalculateTotal(){
console.log(" 'calculateTotal rolled back' ");
};
function rollbackUpdateCouponUsage(){
console.log(" 'couponUsage rolled back' ");
};
function rollbackChargeVendorProcessingFee(){
console.log(" 'vendorFeeCharge rolled back' ");
};