-
Notifications
You must be signed in to change notification settings - Fork 2
/
await.ts
334 lines (321 loc) · 14 KB
/
await.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
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
/*
* @nevware21/ts-async
* https://github.com/nevware21/ts-async
*
* Copyright (c) 2022 NevWare21 Solutions LLC
* Licensed under the MIT license.
*/
import { isPromiseLike } from "@nevware21/ts-utils";
import { AwaitResponse } from "../interfaces/await-response";
import { IPromise } from "../interfaces/IPromise";
import { FinallyPromiseHandler, RejectedPromiseHandler, ResolvedPromiseHandler } from "../interfaces/types";
import { REJECTED } from "../internal/constants";
/**
* Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
* @group Await Helper
* @param value - The value or promise like value to wait
* @param cb - The callback function to call with the resulting value, if the value is not a
* promise like value then the callback is called synchronously, if the value is a promise then
* the callback will be called once the promise completes the resulting value will be passed as an
* IAwaitResponse instance, it will be called whether any promise resolves or rejects.
* @returns The value returned by the `cb` callback function, if the value is a promise then the return value
* of the callback will be returned as a promise whether the callback returns a promise or not.
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via doAwaitResponse
* doAwaitResponse(promise, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // Do something with the reason
* }
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwaitResponse(42, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // This will never be true as the value is not a promise
* }
* });
* ```
*/
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>, cb: (response: AwaitResponse<T | TResult1>) => T | TResult1 | TResult2 | Promise<T | TResult1 | TResult2>): T | TResult1 | TResult2 | Promise<T | TResult1 | TResult2>;
/**
* Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param cb - The callback function to call with the resulting value, if the value is not a
* promise like value then the callback is called synchronously, if the value is a promise then
* the callback will be called once the promise completes the resulting value will be passed as an
* IAwaitResponse instance, it will be called whether any promise resolves or rejects.
* @returns The value returned by the `cb` callback function, if the value is a promise then the return value
* of the callback will be returned as a promise whether the callback returns a promise or not.
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via doAwaitResponse
* doAwaitResponse(promise, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // Do something with the reason
* }
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwaitResponse(42, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // This will never be true as the value is not a promise
* }
* });
* ```
*/
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | PromiseLike<T>, cb: (response: AwaitResponse<T | TResult1>) => T | TResult1 | TResult2 | PromiseLike<T | TResult1 | TResult2>): T | TResult1 | TResult2 | PromiseLike<T | TResult1 | TResult2>;
/**
* Helper to coallesce the promise resolved / reject into a single callback to simplify error handling.
* @group Await Helper
* @param value - The value or promise like value to wait to be resolved or rejected.
* @param cb - The callback function to call with the resulting value, if the value is not a
* promise like value then the callback is called synchronously, if the value is a promise then
* the callback will be called once the promise completes the resulting value will be passed as an
* IAwaitResponse instance, it will be called whether any promise resolves or rejects.
* @returns The value returned by the `cb` callback function, if the value is a promise then the return value
* of the callback will be returned as a promise whether the callback returns a promise or not.
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via doAwaitResponse
* doAwaitResponse(promise, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // Do something with the reason
* }
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwaitResponse(42, (value) => {
* if (!value.rejected) {
* // Do something with the value
* } else {
* // This will never be true as the value is not a promise
* }
* });
* ```
*/
export function doAwaitResponse<T, TResult1 = T, TResult2 = never>(value: T | IPromise<T>, cb: (response: AwaitResponse<T | TResult1>) => T | TResult1 | TResult2 | IPromise<T | TResult1 | TResult2>): T | TResult1 | TResult2 | IPromise<T | TResult1 | TResult2> {
return doAwait(value as T, (value) => {
return cb ? cb({
status: "fulfilled",
rejected: false,
value: value
}) : value;
},
(reason) => {
return cb ? cb({
status: REJECTED,
rejected: true,
reason: reason
}) : reason;
});
}
/**
* Wait for the promise to resolve or reject, if resolved the callback function will be called with it's value and if
* rejected the rejectFn will be called with the reason. If the passed promise argument is not a promise the callback
* will be called synchronously with the value.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param resolveFn - The callback to call on the promise successful resolving.
* @param rejectFn - The callback to call when the promise rejects
* @param finallyFn - The callback to call once the promise has resolved or rejected
* @returns The passed value, if it is a promise and there is either a resolve or reject handler
* then it will return a chained promise with the value from the resolve or reject handler (depending
* whether it resolve or rejects)
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via a chained promise
* let chainedPromise = promise.then((value) => {
* // Do something with the value
* });
*
* // Handle via doAwait
* doAwait(promise, (value) => {
* // Do something with the value
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwait(42, (value) => {
* // Do something with the value
* });
* ```
*/
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | Promise<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): TResult1 | TResult2 | Promise<TResult1 | TResult2>;
/**
* Wait for the promise to resolve or reject, if resolved the callback function will be called with it's value and if
* rejected the rejectFn will be called with the reason. If the passed promise argument is not a promise the callback
* will be called synchronously with the value.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param resolveFn - The callback to call on the promise successful resolving.
* @param rejectFn - The callback to call when the promise rejects
* @param finallyFn - The callback to call once the promise has resolved or rejected
* @returns The passed value, if it is a promise and there is either a resolve or reject handler
* then it will return a chained promise with the value from the resolve or reject handler (depending
* whether it resolve or rejects)
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via a chained promise
* let chainedPromise = promise.then((value) => {
* // Do something with the value
* });
*
* // Handle via doAwait
* doAwait(promise, (value) => {
* // Do something with the value
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwait(42, (value) => {
* // Do something with the value
* });
* ```
*/
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | PromiseLike<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): TResult1 | TResult2 | PromiseLike<TResult1 | TResult2>;
/**
* Wait for the promise to resolve or reject, if resolved the callback function will be called with it's value and if
* rejected the rejectFn will be called with the reason. If the passed promise argument is not a promise the callback
* will be called synchronously with the value.
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param resolveFn - The callback to call on the promise successful resolving.
* @param rejectFn - The callback to call when the promise rejects
* @param finallyFn - The callback to call once the promise has resolved or rejected
* @returns The passed value, if it is a promise and there is either a resolve or reject handler
* then it will return a chained promise with the value from the resolve or reject handler (depending
* whether it resolve or rejects)
* @example
* ```ts
* let promise = createPromise<number>((resolve, reject) => {
* resolve(42);
* });
*
* // Handle via a chained promise
* let chainedPromise = promise.then((value) => {
* // Do something with the value
* });
*
* // Handle via doAwait
* doAwait(promise, (value) => {
* // Do something with the value
* });
*
* // It can also handle the raw value, so you could process the result of either a
* // synchrounous return of the value or a Promise
* doAwait(42, (value) => {
* // Do something with the value
* });
* ```
*/
export function doAwait<T, TResult1 = T, TResult2 = never>(value: T | IPromise<T>, resolveFn: ResolvedPromiseHandler<T, TResult1>, rejectFn?: RejectedPromiseHandler<TResult2>, finallyFn?: FinallyPromiseHandler): TResult1 | TResult2 | IPromise<TResult1 | TResult2> {
let result: T | TResult1 | TResult2 | IPromise<T | TResult1 | TResult2> | PromiseLike<TResult1 | TResult2> = value;
try {
if (isPromiseLike<T>(value)) {
if (resolveFn || rejectFn) {
result = value.then(resolveFn, rejectFn) as any;
}
} else {
try {
if (resolveFn) {
result = resolveFn(value);
}
} catch (err) {
if (rejectFn) {
result = rejectFn(err);
} else {
throw err;
}
}
}
} finally {
if (finallyFn) {
doFinally(result as any, finallyFn);
}
}
return result as any;
}
/**
* Wait for the promise to resolve or reject and then call the finallyFn. If the passed promise argument is not a promise the callback
* will be called synchronously with the value. If the passed promise doesn't implement finally then a finally implementation will be
* simulated using then(..., ...).
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param finallyFn - The finally function to call once the promise has resolved or rejected
*/
export function doFinally<T>(value: T | Promise<T>, finallyFn: FinallyPromiseHandler): T | Promise<T>;
/**
* Wait for the promise to resolve or reject and then call the finallyFn. If the passed promise argument is not a promise the callback
* will be called synchronously with the value. If the passed promise doesn't implement finally then a finally implementation will be
* simulated using then(..., ...).
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param finallyFn - The finally function to call once the promise has resolved or rejected
*/
export function doFinally<T>(value: T | PromiseLike<T>, finallyFn: FinallyPromiseHandler): T | PromiseLike<T>;
/**
* Wait for the promise to resolve or reject and then call the finallyFn. If the passed promise argument is not a promise the callback
* will be called synchronously with the value. If the passed promise doesn't implement finally then a finally implementation will be
* simulated using then(..., ...).
* @group Await Helper
* @param value - The value or promise like value to wait for
* @param finallyFn - The finally function to call once the promise has resolved or rejected
*/
export function doFinally<T>(value: T | IPromise<T>, finallyFn: FinallyPromiseHandler): T | IPromise<T> {
let result = value;
if (finallyFn) {
if (isPromiseLike<T>(value)) {
if ((value as IPromise<T>).finally) {
result = (value as IPromise<T>).finally(finallyFn);
} else {
// Simulate finally if not available
result = value.then(
function(value) {
finallyFn();
return value;
}, function(reason: any) {
finallyFn();
throw reason;
});
}
} else {
finallyFn();
}
}
return result;
}