-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (174 loc) · 5 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
function resolve (value) {
let _self = this
setTimeout(function () {
if (_self.state === 'pending') {
_self.state = 'fullfilled'
_self.value = value
_self.resolveCallbacks.forEach((item) => {
item(_self.value)
})
}
})
}
function reject (error) {
let _self = this
setTimeout(function () {
if (_self.state === 'pending') {
_self.state = 'rejected'
_self.value = error
// 用于处理promise调用链兜底错误提示
if (_self.rejectCallbacks.length === 0) {
console.error(error)
}
_self.rejectCallbacks.forEach((item) => {
item(_self.value)
})
}
})
}
// 用于结束promise
function end () {
return new Promise(function () {})
}
// 遵循promise/A+规范的promise解析过程
function promiseResolve (newpromise, x, resolve, reject) {
if (newpromise === x) {
return reject(new TypeError('promise has been detected'))
}
if (x instanceof Promise) {
return x.then(resolve, reject)
}
if ((x !== null) && (typeof x === 'function' || typeof x === 'object')) {
let calledOrThrowThen = false
try {
let then = x.then
if (typeof then === 'function') {
then.call(x, function (y) {
if (calledOrThrowThen) return
calledOrThrowThen = true
return promiseResolve(newpromise, y, resolve, reject)
}, function (r) {
if (calledOrThrowThen) return
calledOrThrowThen = true
reject(r)
})
} else {
return resolve(x)
}
} catch (e) {
if (calledOrThrowThen) return
calledOrThrowThen = true
return reject(e)
}
} else {
return resolve(x)
}
}
function parseResult (type, onFullfilled, onRejected) {
let _self = this
let newpromise = new Promise(function (resolve, reject) {
// 异步处理onFullfilled与onRejected
setTimeout(function () {
try {
let onRes = type === 'resolve' ? onFullfilled(_self.value) : onRejected(_self.value)
promiseResolve(newpromise, onRes, resolve, reject)
} catch (e) {
reject(e)
}
})
})
return newpromise
}
function processThenFill (onFullfilled, onRejected) {
return parseResult.call(this, 'resolve', onFullfilled, onRejected)
}
function processThenReject (onFullfilled, onRejected) {
return parseResult.call(this, 'reject', onFullfilled, onRejected)
}
function processThenPending (onFullfilled, onRejected) {
let _self = this
return new Promise(function (resolve, reject) {
_self.resolveCallbacks.push(function () {
parseResult.call(_self, 'resolve', onFullfilled, onRejected, resolve, reject)
})
_self.rejectCallbacks.push(function () {
parseResult.call(_self, 'reject', onFullfilled, onRejected, resolve, reject)
})
})
}
export default class Promise {
constructor (fn) {
this.state = 'pending'
this.value = ''
this.resolveCallbacks = []
this.rejectCallbacks = []
if (typeof fn !== 'function') {
throw new Error('promise constructor must accept a function type param')
}
try {
fn.call(this, resolve.bind(this), reject.bind(this))
} catch (e) {
reject.call(this, e)
}
}
then (onFullfilled, onRejected) {
onFullfilled = onFullfilled && (typeof onFullfilled === 'function') ? onFullfilled : (value) => value
onRejected = onRejected && (typeof onRejected === 'function') ? onRejected : (error) => { throw new Error(error) }
if (this.state === 'fullfilled') {
return processThenFill.call(this, onFullfilled, onRejected)
}
if (this.state === 'rejected') {
return processThenReject.call(this, onFullfilled, onRejected)
}
if (this.state === 'pending') {
return processThenPending.call(this, onFullfilled, onRejected)
}
}
catch (onRejected) {
return this.then(null, onRejected)
}
end () {
return end()
}
static resolve (value) {
let newpromise = new Promise(function (resolve, reject) {
promiseResolve(newpromise, value, resolve, reject)
})
return newpromise
}
static race (promiseArrays) {
if (!Array.isArray(promiseArrays)) {
throw new Error('Promise.race must accept an array as the param')
}
return new Promise(function (resolve, reject) {
promiseArrays.forEach((item) => {
Promise.resolve(item).then((value) => {
resolve(value)
}, (error) => {
reject(error)
})
})
})
}
static all (promiseArrays) {
if (!Array.isArray(promiseArrays)) {
throw new Error('Promise.race must accept an array as the param')
}
let promiseLength = promiseArrays.length
let resolveNumbers = 0
let resolveValues = []
return new Promise(function (resolve, reject) {
promiseArrays.forEach((item) => {
Promise.resolve(item).then((value) => {
resolveValues.push(value)
resolveNumbers++
if (resolveNumbers === promiseLength) {
resolve(resolveValues)
}
}, (error) => {
reject(error)
})
})
})
}
}