-
Notifications
You must be signed in to change notification settings - Fork 0
/
Promise.js
198 lines (180 loc) · 5.3 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
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
const validStates = {
PENDING: 'PENDING',
FULFILLED: 'FULFILLED',
REJECTED: 'REJECTED'
};
const isFunction = (fn) => typeof fn === 'function';
const defaultFulfill = (value) => value;
const defaultReject = (reason) => {
throw reason;
};
const noop = () => {
};
const asyncFy = (fn, oThis) => {
const orgFn = fn;
let syncFlag = setTimeout(() => {
syncFlag = null;
if (fn) {
fn();
}
});
fn = null;
return (...args) => {
if (syncFlag) {
return fn = orgFn.bind(oThis, ...args);
}
return orgFn.apply(oThis, args);
};
};
class Clark {
constructor(fn) {
this.state = validStates.PENDING;
this.handlers = {
onFulfilled: defaultFulfill,
onRejected: defaultReject
};
this.queue = [];
if (!isFunction(fn)) {
throw TypeError('expect function as parameter');
}
fn.call(this, (value) => this.resolve(value), (reason) => this.reject(reason));
this.processQueue();
}
static resolve(value) {
if (value instanceof this.prototype.constructor) {
return value;
} else if (value && isFunction(value.then)) {
const promise = new Clark(noop);
try {
value.then((x) => promise.resolve(x), (reason) => promise.reject(reason));
} catch (e) {
promise.reject(e);
}
return promise;
}
return new Clark((resolve) => resolve(value));
}
static reject(value) {
return new Clark((resolve, reject) => reject(value));
}
static race(promises) {
if (!Array.isArray(promises)) {
promises = [promises];
}
return new Clark((resolve, reject) => {
for (const promise of promises) {
Clark.resolve(promise).then(resolve, reject);
}
});
}
static all(promises) {
if (!Array.isArray(promises)) {
promises = [promises];
}
let count = 0;
const result = [], length = promises.length;
return new Clark((resolve, reject) => {
promises.forEach((promise, index) => {
Clark.resolve(promise).then((value) => {
result[index] = value;
if (++count === length) {
resolve(result);
}
}, (err) => reject(err));
});
});
}
static map(data, fn) {
return Clark.all(data.map((item, index) => new Clark((resolve) => {
if (fn.length === 3) {
return fn.call(null, item, index, resolve);
}
return fn.call(null, item, resolve);
})));
}
static waterfall(...fns) {
const length = fns.length;
let index = 0;
return (function handleNext(data) {
return new Clark((resolve) => {
fns[index++].call(null, data ? data : resolve, resolve);
}).then((value) => {
if (!Array.isArray(value)) {
value = [value];
}
if (index >= length) {
return value;
}
return handleNext(value);
});
})();
}
reject(reason) {
if (this.state !== validStates.PENDING) {
return;
}
this.state = validStates.REJECTED;
this.reason = reason;
this.processQueue();
}
fulfill(value) {
if (this.state !== validStates.PENDING) {
return;
}
this.state = validStates.FULFILLED;
this.value = value;
this.processQueue();
}
resolve(value) {
if (value instanceof this.constructor || (value && isFunction(value.then))) {
try {
value.then((x) => this.resolve(x), (reason) => this.reject(reason));
} catch (e) {
this.reject(e);
}
return value;
}
return this.fulfill(value);
}
then(resolve, reject) {
const promise = new Clark(noop);
if (isFunction(resolve)) {
promise.handlers.onFulfilled = resolve;
}
if (isFunction(reject)) {
promise.handlers.onRejected = reject;
}
this.queue.push(promise);
this.processQueueAsync();
return promise;
}
processQueueAsync() {
asyncFy(this.processQueue, this)();
}
processQueue() {
let handler, value, attr;
if (this.state === validStates.REJECTED) {
handler = 'onRejected';
attr = 'reason';
} else if (this.state === validStates.FULFILLED) {
handler = 'onFulfilled';
attr = 'value';
} else {
return;
}
const pendingQueue = this.queue.filter((item) => item.state === validStates.PENDING);
for (const promiseInstance of pendingQueue) {
try {
value = promiseInstance.handlers[handler](this[attr]);
} catch (e) {
promiseInstance.reject(e);
continue;
}
promiseInstance.resolve(value);
}
}
catch(fn) {
return this.then(null, fn);
}
}
module.exports = Clark;