-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo12_async_promise_1.js
99 lines (85 loc) · 2 KB
/
Demo12_async_promise_1.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
/**
* * Resolve call-back hell problem
* * ES using PROMISE
*/
'use strict'
console.clear()
//? Problem statement
const borrow_money = () => {
console.log('borrow money')
return 100
}
const hold_birthday_of_girlfriend = (money) => {
console.log('hold birthday of girl friend')
return ('OK with' + money)
}
const go_to_relax = () => { console.log('Go to BAR...')}
let money = borrow_money()
let girl_friend_birthday = hold_birthday_of_girlfriend(money)
let result_after_birthday = go_to_relax()
console.log('------------------------------------------')
//? Promise status
// - Pending
// - Fulfilled (resolve)
// - Rejected
//? Promise properties
// - constructor(callback)
//? Promise methods
// - then(onFulfilled, onRejected)
// - catch(onRejected)
// - finally(onFinally)
console.log('--- Example 01 ---')
const p_1 = new Promise((resolve, reject) => {
resolve('P_1 is resolved')
})
const p_2 = new Promise((resolve, reject) => {
reject('P_2 is rejected')
})
p_1.then(success => {
console.log('P_1 is success', success)
})
p_2.catch(error => {
console.log('P_2 is rejected', error)
})
p_1.then(success => {
console.log(success)
})
.catch(error => {
console.log(error)
})
.finally(() => {
console.log('=> Finally')
})
console.log('--- Example 02 ---')
const return_money = () => {
console.log('Return money')
return new Promise((resolve, reject) => {
let isHappy = Math.random() >= 0.5
if (isHappy)
return resolve(1000)
else
return reject('Do not happy, do not return')
})
}
return_money()
.then(money => {
setTimeout(()=>{
console.log('Money is return', money)
}, 500)
})
.then(() => {
setTimeout(() => {
console.log('Use money to hold party,', money)
}, 300)
})
.then(() => {
setTimeout(() => {
console.log('Go to BAR')
}, 100)
})
.catch(debt => {
console.log('Money go out forever ^_^! because', debt)
})
.finally(() => {
console.log('-- GO HOME --')
})