-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpromisechain.js
44 lines (41 loc) · 1.2 KB
/
promisechain.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
function xayCaPhe() {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('Bước 1: Chuẩn bị xay cà phê');
resolve({cafe: ['Robusta', 'Arabica']});
console.log('Bước 1: DONE');
}, 2000);
});
}
function vatSua(dataResolveMot) {
const promise = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("Bước 2: Lấy sữa + trộn với cà phê");
resolve({newData: dataResolveMot.cafe[0] + ' + sữa ông thọ'});
console.log('Bước 2: DONE');
}, 2000);
});
return promise;
};
function phaCaPhe(dataResolveHai) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("Bước 3: Lấy sữa + trộn với cà phê + pha cafe phê");
resolve({result: dataResolveHai.newData + "+ pha bằng phin!"});
console.log('Bước 3: DONE');
}, 3000);
});
};
xayCaPhe().
then(vatSua).
then(phaCaPhe)
.then(function(cafePhaXong){
console.log(cafePhaXong)
});
// async function done(){
// const xay = await xayCaPhe();
// const sua = await vatSua(xay);
// const ketqua = await phaCaPhe(sua);
// console.log(ketqua)
// }
// done()