-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.promise.js
48 lines (41 loc) · 1.16 KB
/
2.promise.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
function tra_tien_em_anh_oi() {
return new Promise(function(resolve, reject) {
var isHappy = Math.random() >= 0.5;
if (isHappy) {
var tien = 1000;
return resolve(tien); // Promise dc fulfilled
}
var reason = 'lịt pẹ bố dek trả đấy làm gì nhau';
reject(reason); // Promise ở trạng thái reject
});
}
function nhau_an_mung(tien) {
console.log("Tien nhau duoc: " + tien);
return new Promise(function(resolve, reject) {
var tienMaxa = tien - 400;
return resolve(tienMaxa)
})
}
function mat_xa(tien) {
console.log("Tien mat xa duoc: " + tien);
return new Promise(function(resolve, reject) {
var tienConLai = 0;
return resolve(tienConLai)
})
}
function hue_oi(tien) {
console.log("Tien con lai " + tien);
}
tra_tien_em_anh_oi()
.then(function(tiennhau) {
return nhau_an_mung(tiennhau);
})
.then(function(tienmatxa) {
return mat_xa(tienmatxa);
})
.then(function(tienconlai) { // het tien
return hue_oi(tienconlai);
})
.catch(function(reason) {
console.log(reason);
});