-
Notifications
You must be signed in to change notification settings - Fork 3
/
FSPromise.ts
296 lines (226 loc) · 8.24 KB
/
FSPromise.ts
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
'use strict';
export class FSPromiseCancelError extends Error {
name: string;
constructor(message?: string) {
super(message);
this.name = 'FSPromiseCancelError';
}
}
var Async = false;
var isNextTick = (typeof (global) === 'object');
export function setAsync(isAsync: boolean) {
Async = isAsync;
}
export class FSPromise<R> implements PromiseLike<R> {
private internalPromise: Promise<R>;
private parentPromise: FSPromise<R>;
private isAbort: boolean;
private abortError: FSPromiseCancelError;
/**
* If you call resolve in the body of the callback passed to the constructor,
* your promise is fulfilled with result object passed to resolve.
* If you call reject your promise is rejected with the object passed to resolve.
* For consistency and debugging (eg stack traces), obj should be an instanceof Error.
* Any errors thrown in the constructor callback will be implicitly passed to reject().
*/
constructor(callback: (resolve: (value?: R | PromiseLike<R>) => void, reject: (error?: any) => void) => void) {
this.isAbort = false;
this.internalPromise = new Promise((resolve, reject) => {
let doCallback = () => {
try {
callback((value) => {
if (this.isAbort) {
return reject(this.abortError);
}
resolve(value);
}, (value) => {
reject(value);
});
} catch (e) {
reject(e);
}
};
if (Async) {
if (isNextTick) {
process.nextTick(doCallback);
} else {
setTimeout(doCallback, 0);
}
} else {
doCallback();
}
});
this.internalPromise.catch((e) => {
/** */
})
}
/**
* onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
* Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
* Both callbacks have a single parameter , the fulfillment value or rejection reason.
* "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
* If an error is thrown in the callback, the returned promise rejects with that error.
*
* @param onFulfilled called when/if "promise" resolves
* @param onRejected called when/if "promise" rejects
*/
then<U>(onFulfilled?: (value: R) => U | PromiseLike<U>, onRejected?: (error: any) => U | PromiseLike<U>): FSPromise<U> {
let promise = new FSPromise<any>((resolve, reject) => {
let doCallback = () => {
this.internalPromise.then((value: R) => {
if (this.isAbort) {
reject(this.abortError);
if (onRejected) {
onRejected(this.abortError);
}
return;
}
if (!onFulfilled) {
resolve(value);
return;
}
try {
let returnValue: U | PromiseLike<U> = onFulfilled(value);
resolve(returnValue);
} catch (e) {
reject(e);
}
}, (error) => {
if (this.isAbort) {
reject(this.abortError);
if (onRejected) {
onRejected(this.abortError);
}
return;
}
if (!onRejected) {
reject(error);
return;
}
try {
let returnValue: U | PromiseLike<U> = onRejected(error);
resolve(returnValue);
} catch (e) {
reject(e);
}
});
}
if (Async) {
if (isNextTick) {
process.nextTick(doCallback);
} else {
setTimeout(doCallback, 0);
}
} else {
doCallback();
}
});
promise.parentPromise = this;
return promise;
}
/**
* Sugar for promise.then(undefined, onRejected)
*
* @param onRejected called when/if "promise" rejects
*/
catch<U>(onRejected?: (error: any) => U | PromiseLike<U>): FSPromise<U> {
return this.then(null, onRejected);
}
/**
* Trigger an catchable FSPromiseCancelError and stop execution of Promise
*/
abort(): void {
this._abort(new FSPromiseCancelError('Cancel'));
}
private _abort(abortError: FSPromiseCancelError): void {
this.abortError = abortError;
this.isAbort = true;
if (!!this.parentPromise) {
this.parentPromise._abort(this.abortError);
}
}
/**
* Make a new promise from the PromiseLike.
* A PromiseLike is promise-like in as far as it has a "then" method.
*/
public static resolve<R>(value?: R | PromiseLike<R>): FSPromise<R> {
return new FSPromise((resolve, reject) => {
resolve(value);
});
}
/**
* Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error
*/
public static reject(error: any): FSPromise<any> {
return new FSPromise((resolve, reject) => {
reject(error);
});
}
/**
* Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects.
* the array passed to all can be a mixture of promise-like objects and other objects.
* The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value.
*/
public static all<R>(promises: (R | PromiseLike<R>)[]): FSPromise<R[]> {
let promise = new FSPromise<R[]>((resolve, reject) => {
let doCallback = () => {
Promise.all(promises).then((value) => {
if (promise.isAbort) {
reject(promise.abortError);
return;
}
resolve(value);
}, (error) => {
if (promise.isAbort) {
reject(promise.abortError);
return;
}
reject(error);
});
};
if (Async) {
if (isNextTick) {
process.nextTick(doCallback);
} else {
setTimeout(doCallback, 0);
}
} else {
doCallback();
}
});
return promise;
}
/**
* Make a Promise that fulfills when any item fulfills, and rejects if any item rejects.
*/
public static race<R>(promises: (R | PromiseLike<R>)[]): FSPromise<R> {
let promise = new FSPromise<R>((resolve, reject) => {
let doCallback = () => {
Promise.race(promises).then((value) => {
if (promise.isAbort) {
reject(promise.abortError);
return;
}
resolve(value);
}, (error) => {
if (promise.isAbort) {
reject(promise.abortError);
return;
}
reject(error);
});
};
if (Async) {
if (isNextTick) {
process.nextTick(doCallback);
} else {
setTimeout(doCallback, 0);
}
} else {
doCallback();
}
});
return promise;
}
}
export default FSPromise;