-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.v1.0.0.js
162 lines (143 loc) · 4.54 KB
/
promise.v1.0.0.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const STATE_PENDING = 'pending'
const STATE_FULFILLED = 'fulfilled'
const STATE_REJECTED = 'rejected'
try {
if (!window.process) {
window.process = null
}
} catch (e) {
// ignore
}
const nextTick = func => {
if (process && process.nextTick) {
process.nextTick(func);
} else {
// TODO: 如果是在浏览器环境中,可以用 MutationObserver 替代 setTimeout
setTimeout(func);
}
}
function isFunction(o) {
return typeof o === 'function'
}
function isThenable(o, p) {
return ['object', 'function'].indexOf(typeof o) >= 0 && isFunction(p)
}
function Promise(func) {
this.state = STATE_PENDING
this.result = null
this.consumerType = null
this.consumer = null
this.listeners = []
this.isWaiting = false
const setAndBroadcast = (r, s) => {
if (this.state !== STATE_PENDING || this.isWaiting) {
return
}
if (s === STATE_FULFILLED && r instanceof Promise) {
this.consumerType = STATE_FULFILLED
this.consumer = re => re
this.isWaiting = true
r.register(this)
} else {
this.result = r
this.state = s
nextTick(_ => {
if (this.listeners.length) {
while (this.listeners.length > 0) {
this.listeners.shift().accept(this)
}
} else if (this.state === STATE_REJECTED) {
// throw this.result
}
})
}
}
const resolve = (r) => {
setAndBroadcast(r, STATE_FULFILLED)
}
const reject = (e) => {
setAndBroadcast(e, STATE_REJECTED)
}
const acceptNow = (func) => {
this.isWaiting = false
func()
}
this.accept = (p) => {
let r = p.result
if (this.consumer) {
const consumer = this.consumer
if (this.consumerType === null) {
nextTick(_ => {
acceptNow(_ => {
consumer()
setAndBroadcast(r, p.state)
})
})
} else if (this.consumerType === p.state) {
nextTick(_ => {
acceptNow(_ => {
try {
r = consumer(p.result)
if (r === this) {
reject(new TypeError('`promise` and `x` must not refer to the same object'))
} else {
if (r && !(r instanceof Promise)) {
const thenProp = r.then
if (isThenable(r, thenProp)) {
const rt = r
r = new Promise((resolve, reject) => {
thenProp.call(rt, resolve, reject)
})
}
}
resolve(r)
}
} catch (e) {
reject(e)
}
})
})
} else {
acceptNow(_ => {
setAndBroadcast(r, p.state)
})
}
}
}
try {
func(resolve, reject)
} catch (e) {
reject(e)
}
}
Promise.prototype.register = function (np) {
if (this.state === STATE_PENDING) {
this.listeners.push(np)
} else {
np.accept(this)
}
}
Promise.prototype.next = function (func, type) {
let np = new Promise(_ => {
})
np.consumerType = type
np.consumer = func
this.register(np)
return np
}
Promise.prototype.then = function (onFulfilled, onRejected) {
let result = this.next(isFunction(onFulfilled) ? onFulfilled : r => r, STATE_FULFILLED)
if (isFunction(onRejected)) {
result = result.next(onRejected, STATE_REJECTED)
}
return result
}
Promise.prototype.catch = function (onRejected) {
return this.then(null, onRejected)
}
Promise.prototype.finally = function (func) {
return this.next(func, null)
}
Promise.resolve = (r) => new Promise(resolve => resolve(r))
Promise.reject = (e) => new Promise((_, reject) => reject(e))
module.exports = Promise