-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.js
157 lines (137 loc) · 4.77 KB
/
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
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
try {
if (!window.process) {
window.process = null
}
} catch (e) {
// ignore
}
class Promise {
static #STATE_PENDING = 'pending'
static #STATE_FULFILLED = 'fulfilled'
static #STATE_REJECTED = 'rejected'
#state = Promise.#STATE_PENDING
#result = null
#consumerType = null
#consumer = null
#listeners = []
#isWaiting = false
constructor(func) {
try {
func(this.#resolve, this.#reject)
} catch (e) {
this.#reject(e)
}
}
then = (onFulfilled, onRejected) => {
let result = this.#next(Promise.#isFunction(onFulfilled) ? onFulfilled : r => r, Promise.#STATE_FULFILLED)
if (Promise.#isFunction(onRejected)) {
result = result.#next(onRejected, Promise.#STATE_REJECTED)
}
return result
}
catch = onRejected => this.then(null, onRejected)
finally = func => this.#next(func, null)
static resolve = (r) => new Promise(resolve => resolve(r))
static reject = (e) => new Promise((_, reject) => reject(e))
static #isFunction = o => typeof o === 'function'
static #isThenable = (o, p) => ['object', 'function'].indexOf(typeof o) >= 0 && Promise.#isFunction(p)
static #nextTick = func => {
if (process && process['nextTick']) {
process['nextTick'](func);
} else {
// TODO: 如果是在浏览器环境中,可以用 MutationObserver 替代 setTimeout
setTimeout(func);
}
}
#setAndBroadcast = (r, s) => {
if (this.#state !== Promise.#STATE_PENDING || this.#isWaiting) {
return
}
if (s === Promise.#STATE_FULFILLED && r instanceof Promise) {
this.#consumerType = Promise.#STATE_FULFILLED
this.#consumer = re => re
this.#isWaiting = true
r.#register(this)
} else {
this.#result = r
this.#state = s
Promise.#nextTick(_ => {
if (this.#listeners.length) {
while (this.#listeners.length > 0) {
this.#listeners.shift().#accept(this)
}
} else if (this.#state === Promise.#STATE_REJECTED) {
// throw this.result
}
})
}
}
#resolve = (r) => {
this.#setAndBroadcast(r, Promise.#STATE_FULFILLED)
}
#reject = (e) => {
this.#setAndBroadcast(e, Promise.#STATE_REJECTED)
}
#acceptNow = (func) => {
this.#isWaiting = false
func()
}
#accept = (p) => {
let r = p.#result
const consumer = this.#consumer
if (consumer) {
if (this.#consumerType === null) {
Promise.#nextTick(_ => {
this.#acceptNow(_ => {
consumer()
this.#setAndBroadcast(r, p.#state)
})
})
} else if (this.#consumerType === p.#state) {
Promise.#nextTick(_ => {
this.#acceptNow(_ => {
try {
r = consumer(p.#result)
if (r === this) {
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 (Promise.#isThenable(r, thenProp)) {
const rt = r
r = new Promise((resolve, reject) => {
thenProp.call(rt, resolve, reject)
})
}
}
this.#resolve(r)
}
} catch (e) {
this.#reject(e)
}
})
})
} else {
this.#acceptNow(_ => {
this.#setAndBroadcast(r, p.#state)
})
}
}
}
#register = np => {
if (this.#state === Promise.#STATE_PENDING) {
this.#listeners.push(np)
} else {
np.#accept(this)
}
}
#next = (func, type) => {
let np = new Promise(_ => {
})
np.#consumerType = type
np.#consumer = func
this.#register(np)
return np
}
}
module.exports = Promise