-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise-end.js
119 lines (106 loc) · 2.53 KB
/
promise-end.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
// Promise开枝散叶, 传宗接代
function nextTick(func) {
setTimeout(func)
}
class Promise {
constructor (executor) {
this.value = undefined
this.status = 'pending'
this.children = []
executor(value => {
this.setValue(value, 'resolved')
}, reason => {
this.setValue(reason, 'rejected')
})
}
then (onResolved, onRejected) {
var child = new Promise(() => {})
this.children.push(child)
Object.assign(child, {
onResolved: onResolved || (value => value),
onRejected: onRejected || (reason => Promise.reject(reason)) // 返回promise的原因是要改变状态为rejected
})
if (this.status !== 'pending') {
child.triggerHandler(this.value, this.status)
}
return child
}
catch (onRejected) {
return this.then(null, onRejected)
}
format () {
return {
value: this.value,
status: this.status,
children: this.children.map(child => child.format())
}
}
triggerHandler (parentValue, status) {
nextTick(() => {
var handler
if (status === 'resolved') {
handler = this.onResolved
} else if (status === 'rejected') {
handler = this.onRejected
}
this.setValue(handler(parentValue), 'resolved')
})
}
setValue (value, status) {
this.status = status
if (value && value.then) {
value.then(realValue => {
this.setValue(realValue, 'resolved')
}, reason => {
this.setValue(reason, 'rejected')
})
} else {
this.value = value
this.children.forEach(child => {
child.triggerHandler(value, status)
})
}
}
static resolve (value) {
return new Promise(resolve => {
resolve(value)
})
}
static reject (reason) {
return new Promise((resolve, reject) => {
reject(reason)
})
}
static all () { /* TODO */ }
static race () { /* TODO */ }
}
var promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('promise1')
},300)
})
promise.then(value1 => {
console.log('value1',value1)
return new Promise(resolve => {
setTimeout(() => {
resolve(new Promise(resolve2 => {
setTimeout(() => {
resolve2('promise2')
}, 300)
}))
}, 300)
})
}).then(value2 => {
console.log('value2',value2)
value2 = 'promise3'
return value2 //
// throw new Error('这里抛出一个异常e')
}).then(value3 => {
console.log('value3',value3)
return value3
}).catch(err => {
console.log(err)
})
promise.then(value1 => {
console.log('value1',value1)
})