forked from lqt0223/promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (154 loc) · 4.18 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
class Promise {
constructor(handler) {
const ref = this
this.status = 'pending'
this.next = []
// a util function to callback the resolved value of a promise
// the function will modify promise chain to avoid duplicate job scheduling
const _resolve = (p, ref, cb) => {
p.then((value) => {
cb(value)
})
p.next = ref.next
}
// determine of the second value is an immediate value or a promise
// do the sync or async resolution and try to resolve next
const _resolveNext = (ref, nValue) => {
if (ref.next) {
if (!(nValue && nValue.constructor && nValue.constructor.name == 'Promise')) {
resolveNext(ref.next, nValue)
} else {
_resolve(nValue, ref, (nnValue) => {
resolveNext(ref.next, nnValue)
})
}
}
}
// propagate through the promise chain, by invoking the deferred function in sequence
const resolveNext = (ref, value) => {
ref.status = 'resolved'
if (ref.handler) {
try {
var nValue = ref.handler(value)
_resolveNext(ref, nValue)
} catch (e) {
rejectNext(ref, e)
}
}
}
// propagate a error reason through the promise chain, until the first errorHandler is found
// handle the error there and resume promise chain from there
const rejectNext = (ref, reason) => {
if (!ref) {
console.log('UnhandledPromiseRejectionWarning:', reason)
} else {
ref.status = 'rejected'
if (ref.errorHandler) {
try {
var nValue = ref.errorHandler(reason)
_resolveNext(ref, nValue)
} catch (e) {
rejectNext(ref.next, e)
}
} else {
rejectNext(ref.next, reason)
}
}
}
const resolve = (value) => {
ref.status = 'resolved'
if (ref.next && ref.next.length > 0) {
ref.next.forEach((promise) => {
resolveNext(promise, value)
})
}
}
const reject = (reason) => {
ref.status = 'rejected'
if (ref.next) {
rejectNext(ref.next, reason)
}
}
// if the promise constructor is called with a function, execute 'immediately'
if (handler && handler.constructor && handler.constructor.name == 'Function') {
process.nextTick(() => {
try {
handler(resolve, reject)
} catch (e) {
reject(e)
}
})
}
}
then(handler, errorHandler) {
var p = new Promise()
if (handler) {
p.handler = handler
}
if (errorHandler) {
p.errorHandler = errorHandler
}
this.next.push(p)
return p
}
// generate a new promise with the resolve handler that will simply propagate the result
catch(errorHandler) {
return this.then((value) => {
return value
}, errorHandler)
}
static resolve(value) {
return new Promise((resolve) => {
process.nextTick(() => {
resolve(value)
})
})
}
static reject(value) {
return new Promise((resolve, reject) => {
process.nextTick(() => {
reject(value)
})
})
}
static all(promises) {
var len = promises.length
var resolved = 0
var values = new Array(len)
return new Promise((resolve, reject) => {
promises.forEach((promise, index) => {
if (!(promise && promise.constructor && promise.constructor.name == 'Promise')) {
promise = Promise.resolve(promise)
}
promise.then((value) => {
values[index] = value
resolved++
if (resolved == len) {
resolve(values)
}
}).catch((e) => {
reject(e)
})
})
})
}
static race(promises) {
var resolved = 0
return new Promise((resolve, reject) => {
promises.forEach((promise) => {
if (!(promise && promise.constructor && promise.constructor.name == 'Promise')) {
promise = Promise.resolve(promise)
}
promise.then((value) => {
resolved++
if (resolved == 1) {
resolve(value)
}
}).catch((e) => {
reject(e)
})
})
})
}
}
module.exports = Promise