-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.js
376 lines (309 loc) · 8.72 KB
/
p.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* eslint-disable no-extend-native,prefer-promise-reject-errors */
/**
* a implementation of Promise/A+
* @author charlie
* @email [email protected]
*/
const STATE_PENDING = 'pap-pending'
const STATE_FULLFILLED = 'pap-fullfilled'
const STATE_REJECTED = 'pap-rejected'
const NOOP = () => {}
const NOOP_VALUE_THROUGH = (ref) => () => {
if (ref && ref instanceof Promise) {
return ref._value
}
throw new TypeError('value pass through must be with [[promise]] context')
}
const NOOP_ERROR_THROUGH = (ref) => () => {
if (ref && ref instanceof Promise) {
return Promise.reject(ref._value)
}
throw new TypeError('value pass through must be with [[promise]] context')
}
function isFunction (f) {
return typeof f === 'function'
}
function isObject (o) {
return o != null && (typeof o === 'object')
}
function makeSubscriber (onFullfilled, onRejected) {
return {
doResolve: (val) => nextTick(() => onFullfilled(val)), // async
doReject: (err) => nextTick(() => onRejected(err))
}
}
function makeFullfilledValuePromise (value) {
const p = new Promise(NOOP)
p._state = STATE_FULLFILLED
p._value = value
return p
}
function nextTick (cb) {
if (process) {
process.nextTick(cb)
} else {
setImmediate(cb)
}
}
function isPromiseLike (p) {
return p && isFunction(p.then)
}
function checkPromiseInstance (p) {
if (p == null || p.constructor !== Promise) {
throw new TypeError('object must be instanceof Promise .')
}
}
function isIterable (obj) {
// checks for null and undefined
if (obj == null) {
return false
}
return typeof obj[Symbol.iterator] === 'function'
}
function isArrayLike (obj) {
return (
Array.isArray(obj) ||
(!!obj &&
typeof obj === 'object' &&
typeof (obj.length) === 'number' &&
(obj.length === 0 ||
(obj.length > 0 &&
(obj.length - 1) in obj)
)
)
)
}
function checkPromiseStateBlock (p) {
if (p && p._state !== STATE_PENDING) {
throw new Error('[[PromiseState]] was block .')
}
}
function handleChainValue (chainValue, nextResolve, nextReject, prevPromise) {
if (chainValue === prevPromise) {
return nextReject(new TypeError('[[onFullfilled]] can not return the same promise object .'))
} else if (chainValue instanceof Promise) {
chainValue.then(nextResolve, nextReject)
} else if (isObject(chainValue) || isFunction(chainValue)) {
const then = chainValue.then
if (isObject(chainValue) && !isFunction(then)) {
return nextReject(new Error('[[thenable]] object must be with a [[then]] method .'))
}
if (!isFunction(then)) {
return nextResolve(chainValue)
}
try {
then.call(chainValue, nextResolve, nextReject)
} catch (e) {
return nextReject(e)
}
} else {
nextResolve(chainValue)
}
}
/**
* @param executor {Function}
* @constructor
*/
function Promise (executor) {
if (this == null || !isObject(this)) throw new TypeError('Promise must be constructed with new operator .')
if (!isFunction(executor)) throw new TypeError('executor must be Function type .')
if (this.constructor !== Promise) throw new TypeError('Promise must be constructed with new operator .')
if (this instanceof Promise && this._state != null) throw new TypeError('error promise constructed context')
// members
this._state = STATE_PENDING
this._value = null
this._subscribers = []
const doFullfilled = (value) => {
checkPromiseStateBlock(this)
this._value = value
// call subscribers
this._subscribers.forEach((sub) => {
sub.doResolve(value)
})
this._state = STATE_FULLFILLED
}
const doRejected = (error) => {
checkPromiseInstance(this)
this._value = error
// call subscriber
this._subscribers.forEach((sub) => {
sub.doReject(error)
})
this._state = STATE_REJECTED
}
// sync call executor
try {
executor(doFullfilled, doRejected)
} catch (e) {
doRejected(e)
}
}
// static members
const VALUE_PROMISE_NULL = makeFullfilledValuePromise(null)
const VALUE_PROMISE_TRUE = makeFullfilledValuePromise(true)
const VALUE_PROMISE_FALSE = makeFullfilledValuePromise(false)
const VALUE_PROMISE_ZERO = makeFullfilledValuePromise(0)
const VALUE_PROMISE_EMPTY_STRING = makeFullfilledValuePromise('')
Promise.resolve = function (value) {
if (this == null || this !== Promise) {
throw new TypeError('[[resolve]] must be call with Promise context .')
}
// @todo
if (value instanceof Promise) {
return value
}
if (value == null) return VALUE_PROMISE_NULL
if (value === true) return VALUE_PROMISE_TRUE
if (value === false) return VALUE_PROMISE_FALSE
if (value === 0) return VALUE_PROMISE_ZERO
if (value === '') return VALUE_PROMISE_EMPTY_STRING
// check Thenable value
if (isObject(value) || isFunction(value)) {
try {
if (isPromiseLike(value)) {
return new Promise(value.then.bind(value))
}
} catch (e) {
return new Promise((resolve, reject) => {
reject(e)
})
}
}
return makeFullfilledValuePromise(value)
}
Promise.reject = function (value) {
if (this == null || this !== Promise) {
throw new TypeError('[[reject]] must be call with Promise context .')
}
return new Promise((resolve, reject) => {
reject(value)
})
}
Promise.race = function (iterator) {
if (this == null || this !== Promise) {
throw new TypeError('[[race]] must be call with Promise context .')
}
if (!isArrayLike(iterator) && !isIterable(iterator)) {
return Promise.reject(new TypeError('[[race]] non-iterable argument .'))
}
iterator = Array.from(iterator)
if (iterator.length === 0) {
return new Promise(NOOP)
}
// let isSingle = iterator.length === 1
let hadWon = false
let done = NOOP
let fail = NOOP
let p = new Promise((resolve, reject) => {
done = resolve
fail = reject
})
iterator.some(function (item) {
try {
checkPromiseInstance(item)
} catch (e) {
fail(e)
return true
}
item.then(function doneItem (value) {
if (!hadWon) {
hadWon = true
done(value)
}
}, function failItem (error) {
if (!hadWon) {
hadWon = true
fail(error)
}
})
})
return p
}
Promise.all = function (iterator) {
const payloads = []
if (this == null || this !== Promise) {
throw new TypeError('[[all]] must be call with Promise context .')
}
if (!isArrayLike(iterator) && !isIterable(iterator)) {
return Promise.reject(new TypeError('[[all]] parameter must be iterable .'))
}
iterator = Array.from(iterator)
if (iterator.length === 0) {
return makeFullfilledValuePromise(payloads)
}
let done = NOOP
let fail = NOOP
let p = new Promise((resolve, reject) => {
done = resolve
fail = reject
})
// let isSingle = iterator.length === 1
let remains = iterator.length
iterator.some(function (item, index) {
try {
checkPromiseInstance(item)
} catch (e) {
fail(e)
return true
}
item.then(function doneItem (value) {
--remains
payloads[index] = value
if (remains === 0) {
done(payloads)
}
}, function failItem (error) {
// once rejected . all done .
fail(error)
})
})
return p
}
/**
* @param onFullfilled {Function}
* @param onRejected {Function}
* @return {Promise}
*/
Promise.prototype.then = function (onFullfilled, onRejected) {
checkPromiseInstance(this)
!isFunction(onFullfilled) && (onFullfilled = NOOP_VALUE_THROUGH(this))
!isFunction(onRejected) && (onRejected = NOOP_ERROR_THROUGH(this))
// eslint-disable-next-line promise/param-names
return new Promise((nextResolve, nextReject) => {
// do chains @important@
const _chainOnFullfilled = (value) => {
// chain promise state depend on subscriber invoker state
let chainValue
try {
chainValue = onFullfilled(value)
} catch (e) {
return nextReject(e)
}
// handle chain value
handleChainValue(chainValue, nextResolve, nextReject, this)
}
const _chainOnRejected = (error) => {
let chainValue
try {
chainValue = onRejected(error)
} catch (e) {
return nextReject(e)
}
// handle chain value
handleChainValue(chainValue, nextResolve, nextReject, this)
}
// 0. check parent promise state
if (this._state === STATE_FULLFILLED) {
nextTick(() => _chainOnFullfilled(this._value))
} else if (this._state === STATE_REJECTED) {
nextTick(() => _chainOnRejected(this._value))
} else {
this._subscribers.push(makeSubscriber(_chainOnFullfilled, _chainOnRejected))
}
})
}
Promise.prototype.catch = function (onRejected) {
return this.then(null, onRejected)
}
Promise.prototype.finally = function () {}
exports.Promise = Promise