@flex-development/when 1.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +49 -0
- package/README.md +820 -108
- package/dist/index.d.mts +526 -51
- package/dist/lib/index.mjs +5 -1
- package/dist/lib/is-catchable.mjs +24 -0
- package/dist/lib/is-finalizable.mjs +24 -0
- package/dist/lib/is-promise-like.mjs +24 -0
- package/dist/lib/is-promise.mjs +37 -0
- package/dist/lib/is-thenable.mjs +33 -9
- package/dist/lib/when.mjs +102 -19
- package/dist/testing/index.d.mts +149 -0
- package/dist/testing/index.mjs +5 -0
- package/dist/testing/lib/create-thenable.mjs +350 -0
- package/dist/testing/lib/index.mjs +5 -0
- package/package.json +25 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Interfaces - PromiseLike
|
|
3
|
+
* @module when/interfaces/PromiseLike
|
|
4
|
+
*/
|
|
5
|
+
declare global {
|
|
6
|
+
interface PromiseLike<T> {
|
|
7
|
+
/**
|
|
8
|
+
* Attach callbacks for resolution and/or rejection.
|
|
9
|
+
*
|
|
10
|
+
* @template {any} [S=T]
|
|
11
|
+
* The next resolved value on success
|
|
12
|
+
* @template {any} [F=never]
|
|
13
|
+
* The next resolved value on failure
|
|
14
|
+
*
|
|
15
|
+
* @this {unknown}
|
|
16
|
+
*
|
|
17
|
+
* @param {((value: T) => PromiseLike<S> | S) | null | undefined} [succ]
|
|
18
|
+
* The callback to execute when the thenable is resolved
|
|
19
|
+
* @param {((reason: any) => F | PromiseLike<F>) | null | undefined} [fail]
|
|
20
|
+
* The callback to execute when the thenable is rejected
|
|
21
|
+
* @return {PromiseLike<F | S>}
|
|
22
|
+
* The next promise-like object
|
|
23
|
+
*/
|
|
24
|
+
then<S = T, F = never>(succ?: ((value: T) => PromiseLike<S> | S) | null | undefined, fail?: ((reason: any) => F | PromiseLike<F>) | null | undefined): PromiseLike<F | S>;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @file Interfaces - Catchable
|
|
30
|
+
* @module when/interfaces/Catchable
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A thenable that can be caught.
|
|
35
|
+
*
|
|
36
|
+
* @see {@linkcode Thenable}
|
|
37
|
+
*
|
|
38
|
+
* @template {any} [T=any]
|
|
39
|
+
* The resolved value
|
|
40
|
+
*
|
|
41
|
+
* @extends {Thenable<T>}
|
|
42
|
+
*/
|
|
43
|
+
interface Catchable<T = any> extends Thenable<T> {
|
|
44
|
+
/**
|
|
45
|
+
* Attach a callback only to be invoked on rejection.
|
|
46
|
+
*
|
|
47
|
+
* @see {@linkcode Catch}
|
|
48
|
+
*
|
|
49
|
+
* @override
|
|
50
|
+
*/
|
|
51
|
+
catch: Catch<T>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @file Interfaces - Finalizable
|
|
56
|
+
* @module when/interfaces/Finalizable
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A thenable that can be finalized.
|
|
61
|
+
*
|
|
62
|
+
* @see {@linkcode Thenable}
|
|
63
|
+
*
|
|
64
|
+
* @template {any} [T=any]
|
|
65
|
+
* The resolved value
|
|
66
|
+
*
|
|
67
|
+
* @extends {Thenable<T>}
|
|
68
|
+
*/
|
|
69
|
+
interface Finalizable<T = any> extends Thenable<T> {
|
|
70
|
+
/**
|
|
71
|
+
* Attach a callback only to be invoked
|
|
72
|
+
* on settlement (fulfillment or rejection).
|
|
73
|
+
*
|
|
74
|
+
* > 👉 **Note**: The resolved value cannot be modified from the callback.
|
|
75
|
+
*
|
|
76
|
+
* @see {@linkcode Finally}
|
|
77
|
+
*
|
|
78
|
+
* @override
|
|
79
|
+
*/
|
|
80
|
+
finally: Finally<T>;
|
|
81
|
+
}
|
|
82
|
+
|
|
1
83
|
/**
|
|
2
84
|
* @file Interfaces - Options
|
|
3
85
|
* @module when/interfaces/Options
|
|
@@ -10,12 +92,16 @@
|
|
|
10
92
|
* The previously resolved value
|
|
11
93
|
* @template {any} [Next=any]
|
|
12
94
|
* The next resolved value
|
|
95
|
+
* @template {any} [Failure=Next]
|
|
96
|
+
* The next resolved value on failure
|
|
13
97
|
* @template {ReadonlyArray<any>} [Args=any[]]
|
|
14
98
|
* The chain function arguments
|
|
15
|
-
* @template {any} [
|
|
99
|
+
* @template {any} [Error=any]
|
|
100
|
+
* The error to possibly handle
|
|
101
|
+
* @template {any} [This=any]
|
|
16
102
|
* The `this` context
|
|
17
103
|
*/
|
|
18
|
-
interface Options<T = any, Next = any, Args extends readonly any[] = any[],
|
|
104
|
+
interface Options<T = any, Next = any, Failure = Next, Args extends readonly any[] = any[], Error = any, This = any> {
|
|
19
105
|
/**
|
|
20
106
|
* The arguments to pass to the {@linkcode chain} callback.
|
|
21
107
|
*/
|
|
@@ -23,28 +109,201 @@ interface Options<T = any, Next = any, Args extends readonly any[] = any[], Self
|
|
|
23
109
|
/**
|
|
24
110
|
* The chain callback.
|
|
25
111
|
*
|
|
112
|
+
* > 👉 **Note**: For thenables, this callback is passed to `then` as
|
|
113
|
+
* > the `onfulfilled` parameter.
|
|
114
|
+
*
|
|
26
115
|
* @see {@linkcode Chain}
|
|
116
|
+
* @see {@linkcode PromiseLike.then}
|
|
27
117
|
*/
|
|
28
|
-
chain: Chain<T, Next, Args,
|
|
118
|
+
chain: Chain<T, Next, Args, This>;
|
|
29
119
|
/**
|
|
30
|
-
* The `this` context of the {@linkcode chain}
|
|
31
|
-
|
|
120
|
+
* The `this` context of the {@linkcode chain} and {@linkcode fail} callbacks.
|
|
121
|
+
*/
|
|
122
|
+
context?: This | null | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* The callback to fire when a failure occurs.
|
|
125
|
+
*
|
|
126
|
+
* Failures include:
|
|
127
|
+
*
|
|
128
|
+
* - Rejections of the input thenable
|
|
129
|
+
* - Rejections returned from {@linkcode chain}
|
|
130
|
+
* - Synchronous errors thrown in {@linkcode chain}
|
|
131
|
+
*
|
|
132
|
+
* If no `fail` handler is provided, failures are re-thrown or re-propagated.
|
|
133
|
+
*
|
|
134
|
+
* > 👉 **Note**: For thenables, this callback is passed to `then` as
|
|
135
|
+
* > the `onrejected` parameter, and if implemented, to `catch` as well
|
|
136
|
+
* > to prevent unhandled rejections.
|
|
137
|
+
*
|
|
138
|
+
* @see {@linkcode Fail}
|
|
139
|
+
* @see {@linkcode Promise.catch}
|
|
140
|
+
* @see {@linkcode PromiseLike.then}
|
|
141
|
+
*
|
|
142
|
+
* @since 2.0.0
|
|
32
143
|
*/
|
|
33
|
-
|
|
144
|
+
fail?: Fail<Failure, Error, This> | null | undefined;
|
|
34
145
|
/**
|
|
35
|
-
* The callback to
|
|
146
|
+
* The callback to invoke after chaining completes, whether the operation
|
|
147
|
+
* succeeds or fails.
|
|
148
|
+
*
|
|
149
|
+
* It runs exactly once after {@linkcode chain} and {@linkcode fail}, cannot
|
|
150
|
+
* affect the resolved value, and does not intercept errors.
|
|
36
151
|
*
|
|
37
|
-
* @see {@linkcode
|
|
152
|
+
* @see {@linkcode Finish}
|
|
153
|
+
*
|
|
154
|
+
* @since 3.0.0
|
|
38
155
|
*/
|
|
39
|
-
|
|
156
|
+
finish?: Finish<This> | null | undefined;
|
|
40
157
|
}
|
|
41
158
|
|
|
159
|
+
/**
|
|
160
|
+
* @file Interfaces - Thenable
|
|
161
|
+
* @module when/interfaces/Thenable
|
|
162
|
+
*/
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* The completion of an asynchronous operation, and the minimal structural
|
|
166
|
+
* contract required by `when` to treat a value as asynchronous.
|
|
167
|
+
*
|
|
168
|
+
* Unlike {@linkcode PromiseLike}, this interface allows maybe-callable `catch`
|
|
169
|
+
* and `finally` methods, which when present, are used by `when` to ensure
|
|
170
|
+
* failures are handled and post-processing hooks are executed without forcing
|
|
171
|
+
* promise allocation.
|
|
172
|
+
*
|
|
173
|
+
* Maybe-callable methods are named so because they are not required,
|
|
174
|
+
* and may be a method implementation, `null`, or `undefined`.
|
|
175
|
+
*
|
|
176
|
+
* @template {any} [T=any]
|
|
177
|
+
* The resolved value
|
|
178
|
+
*/
|
|
179
|
+
interface Thenable<T = any> {
|
|
180
|
+
/**
|
|
181
|
+
* Attach a callback only to be invoked on rejection.
|
|
182
|
+
*
|
|
183
|
+
* @see {@linkcode Catch}
|
|
184
|
+
*/
|
|
185
|
+
catch?: Catch<T> | null | undefined;
|
|
186
|
+
/**
|
|
187
|
+
* Attach a callback only to be invoked
|
|
188
|
+
* on settlement (fulfillment or rejection).
|
|
189
|
+
*
|
|
190
|
+
* > 👉 **Note**: The resolved value cannot be modified from the callback.
|
|
191
|
+
*
|
|
192
|
+
* @see {@linkcode Finally}
|
|
193
|
+
*/
|
|
194
|
+
finally?: Finally<T> | null | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* Attach callbacks to be invoked on resolution (fulfillment)
|
|
197
|
+
* and/or rejection.
|
|
198
|
+
*
|
|
199
|
+
* @see {@linkcode Then}
|
|
200
|
+
*/
|
|
201
|
+
then: Then<T>;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @file isCatchable
|
|
206
|
+
* @module when/lib/isCatchable
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Check if `value` looks like a `Thenable` that can be caught.
|
|
211
|
+
*
|
|
212
|
+
* @see {@linkcode Catchable}
|
|
213
|
+
*
|
|
214
|
+
* @template {any} T
|
|
215
|
+
* The resolved value
|
|
216
|
+
*
|
|
217
|
+
* @this {void}
|
|
218
|
+
*
|
|
219
|
+
* @param {unknown} value
|
|
220
|
+
* The thing to check
|
|
221
|
+
* @return {value is Catchable<T>}
|
|
222
|
+
* `true` if `value` is a thenable with a `catch` method, `false` otherwise
|
|
223
|
+
*/
|
|
224
|
+
declare function isCatchable<T>(this: void, value: unknown): value is Catchable<T>;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @file isFinalizable
|
|
228
|
+
* @module when/lib/isFinalizable
|
|
229
|
+
*/
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Check if `value` looks like a thenable that can be finalized.
|
|
233
|
+
*
|
|
234
|
+
* @see {@linkcode Finalizable}
|
|
235
|
+
*
|
|
236
|
+
* @template {any} T
|
|
237
|
+
* The resolved value
|
|
238
|
+
*
|
|
239
|
+
* @this {void}
|
|
240
|
+
*
|
|
241
|
+
* @param {unknown} value
|
|
242
|
+
* The thing to check
|
|
243
|
+
* @return {value is Finalizable<T>}
|
|
244
|
+
* `true` if `value` is a thenable with a `finally` method, `false` otherwise
|
|
245
|
+
*/
|
|
246
|
+
declare function isFinalizable<T>(this: void, value: unknown): value is Finalizable<T>;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* @file isPromise
|
|
250
|
+
* @module when/lib/isPromise
|
|
251
|
+
*/
|
|
252
|
+
/**
|
|
253
|
+
* Check if `value` looks like a {@linkcode Promise}.
|
|
254
|
+
*
|
|
255
|
+
* > 👉 **Note**: This function intentionally performs structural checks
|
|
256
|
+
* > instead of brand checks.
|
|
257
|
+
* > It does not rely on `instanceof Promise` or constructors, making it
|
|
258
|
+
* > compatible with cross-realm promises and custom thenables.
|
|
259
|
+
*
|
|
260
|
+
* @see {@linkcode isThenable}
|
|
261
|
+
*
|
|
262
|
+
* @template {any} T
|
|
263
|
+
* The resolved value
|
|
264
|
+
*
|
|
265
|
+
* @this {void}
|
|
266
|
+
*
|
|
267
|
+
* @param {unknown} value
|
|
268
|
+
* The thing to check
|
|
269
|
+
* @param {boolean | null | undefined} [finalizable=true]
|
|
270
|
+
* Whether a `finally` method is required.\
|
|
271
|
+
* When `false`, only `then` and `catch` are checked
|
|
272
|
+
* @return {value is Promise<T>}
|
|
273
|
+
* `true` if `value` is a thenable with a `catch` method,
|
|
274
|
+
* and `finally` method (if requested), `false` otherwise
|
|
275
|
+
*/
|
|
276
|
+
declare function isPromise<T>(this: void, value: unknown, finalizable?: boolean | null | undefined): value is Promise<T>;
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* @file isPromiseLike
|
|
280
|
+
* @module when/lib/isPromiseLike
|
|
281
|
+
*/
|
|
282
|
+
/**
|
|
283
|
+
* Check if `value` looks like a {@linkcode PromiseLike} structure.
|
|
284
|
+
*
|
|
285
|
+
* @template {any} T
|
|
286
|
+
* The resolved value
|
|
287
|
+
*
|
|
288
|
+
* @this {void}
|
|
289
|
+
*
|
|
290
|
+
* @param {unknown} value
|
|
291
|
+
* The thing to check
|
|
292
|
+
* @return {value is PromiseLike<T>}
|
|
293
|
+
* `true` if `value` is an object or function with a `then` method,
|
|
294
|
+
* `false` otherwise
|
|
295
|
+
*/
|
|
296
|
+
declare function isPromiseLike<T>(this: void, value: unknown): value is PromiseLike<T>;
|
|
297
|
+
|
|
42
298
|
/**
|
|
43
299
|
* @file isThenable
|
|
44
300
|
* @module when/lib/isThenable
|
|
45
301
|
*/
|
|
302
|
+
|
|
46
303
|
/**
|
|
47
|
-
* Check if `value` looks like a
|
|
304
|
+
* Check if `value` looks like a {@linkcode Thenable}.
|
|
305
|
+
*
|
|
306
|
+
* @see {@linkcode Thenable}
|
|
48
307
|
*
|
|
49
308
|
* @template {any} T
|
|
50
309
|
* The resolved value
|
|
@@ -53,10 +312,11 @@ interface Options<T = any, Next = any, Args extends readonly any[] = any[], Self
|
|
|
53
312
|
*
|
|
54
313
|
* @param {unknown} value
|
|
55
314
|
* The thing to check
|
|
56
|
-
* @return {value is
|
|
57
|
-
* `true` if `value` is a
|
|
315
|
+
* @return {value is Thenable<T>}
|
|
316
|
+
* `true` if `value` is an object or function with a `then` method,
|
|
317
|
+
* and maybe-callable methods `catch` and/or `finally`, `false` otherwise
|
|
58
318
|
*/
|
|
59
|
-
declare function isThenable<T>(this: void, value: unknown): value is
|
|
319
|
+
declare function isThenable<T>(this: void, value: unknown): value is Thenable<T>;
|
|
60
320
|
|
|
61
321
|
/**
|
|
62
322
|
* @file when
|
|
@@ -65,11 +325,11 @@ declare function isThenable<T>(this: void, value: unknown): value is PromiseLike
|
|
|
65
325
|
|
|
66
326
|
/**
|
|
67
327
|
* Chain a callback, calling the function after `value` is resolved,
|
|
68
|
-
* or immediately if `value` is not thenable.
|
|
328
|
+
* or immediately if `value` is not a thenable.
|
|
69
329
|
*
|
|
70
330
|
* @see {@linkcode Awaitable}
|
|
71
331
|
* @see {@linkcode Chain}
|
|
72
|
-
* @see {@linkcode
|
|
332
|
+
* @see {@linkcode Fail}
|
|
73
333
|
*
|
|
74
334
|
* @template {any} T
|
|
75
335
|
* The previously resolved value
|
|
@@ -77,28 +337,69 @@ declare function isThenable<T>(this: void, value: unknown): value is PromiseLike
|
|
|
77
337
|
* The next resolved value
|
|
78
338
|
* @template {ReadonlyArray<any>} [Args=any[]]
|
|
79
339
|
* The chain function arguments
|
|
80
|
-
* @template {any} [
|
|
340
|
+
* @template {any} [This=any]
|
|
81
341
|
* The `this` context
|
|
342
|
+
* @template {Awaitable<Next>} [Result=Awaitable<Next>]
|
|
343
|
+
* The next awaitable
|
|
82
344
|
*
|
|
83
345
|
* @this {void}
|
|
84
346
|
*
|
|
85
347
|
* @param {Awaitable<T>} value
|
|
86
|
-
* The
|
|
87
|
-
* @param {Chain<T, Next, Args,
|
|
348
|
+
* The current awaitable
|
|
349
|
+
* @param {Chain<T, Next, Args, This>} chain
|
|
88
350
|
* The chain callback
|
|
89
|
-
* @param {
|
|
90
|
-
* The callback to fire when a
|
|
91
|
-
* @param {
|
|
92
|
-
* The `this` context of the chain and
|
|
351
|
+
* @param {null | undefined} [fail]
|
|
352
|
+
* The callback to fire when a failure occurs
|
|
353
|
+
* @param {This | null | undefined} [context]
|
|
354
|
+
* The `this` context of the chain and `fail` callbacks
|
|
93
355
|
* @param {Args} args
|
|
94
356
|
* The arguments to pass to the chain callback
|
|
95
|
-
* @return {
|
|
96
|
-
* The next
|
|
357
|
+
* @return {Result}
|
|
358
|
+
* The next awaitable
|
|
359
|
+
*/
|
|
360
|
+
declare function when<T, Next = any, Args extends any[] = any[], This = unknown, Result extends Awaitable<Next> = Awaitable<Next>>(this: void, value: Awaitable<T>, chain: Chain<T, Next, Args, This>, fail?: null | undefined, context?: This | null | undefined, ...args: Args): Result;
|
|
361
|
+
/**
|
|
362
|
+
* Chain a callback, calling the function after `value` is resolved,
|
|
363
|
+
* or immediately if `value` is not a thenable.
|
|
364
|
+
*
|
|
365
|
+
* @see {@linkcode Awaitable}
|
|
366
|
+
* @see {@linkcode Chain}
|
|
367
|
+
* @see {@linkcode Fail}
|
|
368
|
+
*
|
|
369
|
+
* @template {any} T
|
|
370
|
+
* The previously resolved value
|
|
371
|
+
* @template {any} [Next=any]
|
|
372
|
+
* The next resolved value
|
|
373
|
+
* @template {any} [Failure=Next]
|
|
374
|
+
* The next resolved value on failure
|
|
375
|
+
* @template {ReadonlyArray<any>} [Args=any[]]
|
|
376
|
+
* The chain function arguments
|
|
377
|
+
* @template {any} [Error=any]
|
|
378
|
+
* The error to possibly handle
|
|
379
|
+
* @template {any} [This=any]
|
|
380
|
+
* The `this` context
|
|
381
|
+
* @template {Awaitable<Failure | Next>} [Result=Awaitable<Failure | Next>]
|
|
382
|
+
* The next awaitable
|
|
383
|
+
*
|
|
384
|
+
* @this {void}
|
|
385
|
+
*
|
|
386
|
+
* @param {Awaitable<T>} value
|
|
387
|
+
* The current awaitable
|
|
388
|
+
* @param {Chain<T, Next, Args, This>} chain
|
|
389
|
+
* The chain callback
|
|
390
|
+
* @param {Fail<Failure, Error, This> | null | undefined} [fail]
|
|
391
|
+
* The callback to fire when a failure occurs
|
|
392
|
+
* @param {This | null | undefined} [context]
|
|
393
|
+
* The `this` context of the chain and `fail` callbacks
|
|
394
|
+
* @param {Args} args
|
|
395
|
+
* The arguments to pass to the chain callback
|
|
396
|
+
* @return {Result}
|
|
397
|
+
* The next awaitable
|
|
97
398
|
*/
|
|
98
|
-
declare function when<T, Next = any, Args extends any[] = any[],
|
|
399
|
+
declare function when<T, Next = any, Failure = Next, Args extends any[] = any[], Error = any, This = unknown, Result extends Awaitable<Failure | Next> = Awaitable<Failure | Next>>(this: void, value: Awaitable<T>, chain: Chain<T, Next, Args, This>, fail?: Fail<Failure, Error, This> | null | undefined, context?: This | null | undefined, ...args: Args): Result;
|
|
99
400
|
/**
|
|
100
401
|
* Chain a callback, calling the function after `value` is resolved,
|
|
101
|
-
* or immediately if `value` is not thenable.
|
|
402
|
+
* or immediately if `value` is not a thenable.
|
|
102
403
|
*
|
|
103
404
|
* @see {@linkcode Awaitable}
|
|
104
405
|
* @see {@linkcode Options}
|
|
@@ -107,33 +408,69 @@ declare function when<T, Next = any, Args extends any[] = any[], Self = unknown>
|
|
|
107
408
|
* The previously resolved value
|
|
108
409
|
* @template {any} [Next=any]
|
|
109
410
|
* The next resolved value
|
|
411
|
+
* @template {any} [Failure=Next]
|
|
412
|
+
* The next resolved value on failure
|
|
110
413
|
* @template {ReadonlyArray<any>} [Args=any[]]
|
|
111
414
|
* The chain function arguments
|
|
112
|
-
* @template {any} [
|
|
415
|
+
* @template {any} [Error=any]
|
|
416
|
+
* The error to possibly handle
|
|
417
|
+
* @template {any} [This=unknown]
|
|
113
418
|
* The `this` context
|
|
419
|
+
* @template {Awaitable<Failure | Next>} [Result=Awaitable<Failure | Next>]
|
|
420
|
+
* The next awaitable
|
|
114
421
|
*
|
|
115
422
|
* @this {void}
|
|
116
423
|
*
|
|
117
424
|
* @param {Awaitable<T>} value
|
|
118
|
-
* The
|
|
119
|
-
* @param {Options<T, Next, Args,
|
|
425
|
+
* The current awaitable
|
|
426
|
+
* @param {Options<T, Next, Failure, Args, Error, This>} chain
|
|
120
427
|
* Options for chaining
|
|
121
|
-
* @return {
|
|
122
|
-
* The next
|
|
428
|
+
* @return {Result}
|
|
429
|
+
* The next awaitable
|
|
123
430
|
*/
|
|
124
|
-
declare function when<T, Next = any, Args extends any[] = any[],
|
|
431
|
+
declare function when<T, Next = any, Failure = Next, Args extends any[] = any[], Error = any, This = unknown, Result extends Awaitable<Failure | Next> = Awaitable<Failure | Next>>(this: void, value: Awaitable<T>, chain: Options<T, Next, Failure, Args, Error, This>): Result;
|
|
125
432
|
|
|
126
433
|
/**
|
|
127
434
|
* @file Type Aliases - Awaitable
|
|
128
435
|
* @module when/types/Awaitable
|
|
129
436
|
*/
|
|
437
|
+
|
|
130
438
|
/**
|
|
131
439
|
* A synchronous or thenable value.
|
|
132
440
|
*
|
|
441
|
+
* @see {@linkcode Thenable}
|
|
442
|
+
*
|
|
133
443
|
* @template {any} T
|
|
134
444
|
* The resolved value
|
|
135
445
|
*/
|
|
136
|
-
type Awaitable<T> =
|
|
446
|
+
type Awaitable<T> = Thenable<T> | T;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* @file Type Aliases - Catch
|
|
450
|
+
* @module when/types/Catch
|
|
451
|
+
*/
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Attach a callback only for the rejection of a `Thenable`.
|
|
455
|
+
*
|
|
456
|
+
* @see {@linkcode OnRejected}
|
|
457
|
+
* @see {@linkcode Thenable}
|
|
458
|
+
*
|
|
459
|
+
* @template {any} [T=unknown]
|
|
460
|
+
* The resolved value
|
|
461
|
+
* @template {any} [Reason=any]
|
|
462
|
+
* The reason for the rejection
|
|
463
|
+
* @template {any} [Next=never]
|
|
464
|
+
* The next resolved value
|
|
465
|
+
*
|
|
466
|
+
* @this {any}
|
|
467
|
+
*
|
|
468
|
+
* @param {OnRejected<Next, Reason> | null | undefined} [onrejected]
|
|
469
|
+
* The callback to execute when the thenable is rejected
|
|
470
|
+
* @return {Thenable<Next | T>}
|
|
471
|
+
* The next thenable
|
|
472
|
+
*/
|
|
473
|
+
type Catch<T = unknown, Reason = any> = <Next = never>(this: any, onrejected?: OnRejected<Next, Reason> | null | undefined) => Thenable<Next | T>;
|
|
137
474
|
|
|
138
475
|
/**
|
|
139
476
|
* @file Type Aliases - Chain
|
|
@@ -151,46 +488,184 @@ type Awaitable<T> = PromiseLike<T> | T;
|
|
|
151
488
|
* The next resolved value
|
|
152
489
|
* @template {ReadonlyArray<any>} [Args=any[]]
|
|
153
490
|
* The function arguments
|
|
154
|
-
* @template {any} [
|
|
491
|
+
* @template {any} [This=unknown]
|
|
155
492
|
* The `this` context
|
|
156
493
|
*
|
|
157
|
-
* @this {
|
|
494
|
+
* @this {This}
|
|
158
495
|
*
|
|
159
496
|
* @param {[...Args, T]} params
|
|
160
497
|
* The function parameters, with the last being the previously resolved value.\
|
|
161
498
|
* In cases where a promise is not being resolved,
|
|
162
499
|
* this is the same `value` passed to `when`
|
|
163
500
|
* @return {Awaitable<Next>}
|
|
164
|
-
* The next
|
|
501
|
+
* The next awaitable
|
|
165
502
|
*/
|
|
166
|
-
type Chain<T = any, Next = any, Args extends readonly any[] = any[],
|
|
503
|
+
type Chain<T = any, Next = any, Args extends readonly any[] = any[], This = unknown> = (this: This, ...params: [...Args, T]) => Awaitable<Next>;
|
|
167
504
|
|
|
168
505
|
/**
|
|
169
|
-
* @file Type Aliases -
|
|
170
|
-
* @module when/types/
|
|
506
|
+
* @file Type Aliases - Fail
|
|
507
|
+
* @module when/types/Fail
|
|
171
508
|
*/
|
|
172
509
|
|
|
173
510
|
/**
|
|
174
|
-
* The callback to fire when a
|
|
175
|
-
* or an error is thrown from a synchronous function.
|
|
511
|
+
* The callback to fire when a failure occurs.
|
|
176
512
|
*
|
|
177
513
|
* @see {@linkcode Awaitable}
|
|
178
514
|
*
|
|
179
515
|
* @template {any} [Next=any]
|
|
180
516
|
* The next resolved value
|
|
181
|
-
* @template {any} [
|
|
182
|
-
* The
|
|
183
|
-
* @template {any} [
|
|
517
|
+
* @template {any} [Reason=any]
|
|
518
|
+
* The reason for the failure
|
|
519
|
+
* @template {any} [This=unknown]
|
|
184
520
|
* The `this` context
|
|
185
521
|
*
|
|
186
|
-
* @this {
|
|
522
|
+
* @this {This}
|
|
187
523
|
*
|
|
188
|
-
* @param {
|
|
189
|
-
* The
|
|
524
|
+
* @param {Reason} reason
|
|
525
|
+
* The reason for the failure
|
|
190
526
|
* @return {Awaitable<Next>}
|
|
191
|
-
* The next
|
|
527
|
+
* The next awaitable
|
|
528
|
+
*/
|
|
529
|
+
type Fail<Next = any, Reason = any, This = unknown> = (this: This, reason: Reason) => Awaitable<Next>;
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* @file Type Aliases - Finally
|
|
533
|
+
* @module when/types/Finally
|
|
534
|
+
*/
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Attach a callback that is invoked only when a `Thenable`
|
|
538
|
+
* is settled (fulfilled or rejected).
|
|
539
|
+
*
|
|
540
|
+
* > 👉 **Note**: The resolved value cannot be modified from the callback.
|
|
541
|
+
*
|
|
542
|
+
* @see {@linkcode OnFinally}
|
|
543
|
+
* @see {@linkcode Thenable}
|
|
544
|
+
*
|
|
545
|
+
* @template {any} [T=unknown]
|
|
546
|
+
* The resolved value
|
|
547
|
+
*
|
|
548
|
+
* @this {any}
|
|
549
|
+
*
|
|
550
|
+
* @param {OnFinally | null | undefined} [onfinally]
|
|
551
|
+
* The callback to execute when the thenable is settled
|
|
552
|
+
* @return {Thenable<T>}
|
|
553
|
+
* The next thenable
|
|
554
|
+
*/
|
|
555
|
+
type Finally<T = unknown> = (this: any, onfinally?: OnFinally | null | undefined) => Thenable<T>;
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* @file Type Aliases - Finish
|
|
559
|
+
* @module when/types/Finish
|
|
560
|
+
*/
|
|
561
|
+
/**
|
|
562
|
+
* A post-processing hook invoked exactly once after an awaitable settles,
|
|
563
|
+
* regardless of success or failure.
|
|
564
|
+
*
|
|
565
|
+
* The resolved value cannot be modified from the hook,
|
|
566
|
+
* and any error is re-thrown after execution.
|
|
567
|
+
*
|
|
568
|
+
* @template {any} [This=unknown]
|
|
569
|
+
* The `this` context
|
|
570
|
+
*
|
|
571
|
+
* @this {This}
|
|
572
|
+
*
|
|
573
|
+
* @return {undefined | void}
|
|
574
|
+
*/
|
|
575
|
+
type Finish<This = unknown> = (this: This) => undefined | void;
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* @file Type Aliases - OnFinally
|
|
579
|
+
* @module when/types/OnFinally
|
|
580
|
+
*/
|
|
581
|
+
/**
|
|
582
|
+
* The callback to execute when a `Thenable` is settled (fulfilled or rejected).
|
|
583
|
+
*
|
|
584
|
+
* @this {unknown}
|
|
585
|
+
*
|
|
586
|
+
* @return {undefined | void}
|
|
587
|
+
*/
|
|
588
|
+
type OnFinally = (this: unknown) => undefined | void;
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* @file Type Aliases - OnFulfilled
|
|
592
|
+
* @module when/types/OnFulfilled
|
|
593
|
+
*/
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* The callback to execute when a `Thenable` is resolved.
|
|
597
|
+
*
|
|
598
|
+
* @see {@linkcode Awaitable}
|
|
599
|
+
*
|
|
600
|
+
* @template {any} T
|
|
601
|
+
* The resolved value
|
|
602
|
+
* @template {any} [Next=T]
|
|
603
|
+
* The next resolved value
|
|
604
|
+
*
|
|
605
|
+
* @this {unknown}
|
|
606
|
+
*
|
|
607
|
+
* @param {T} value
|
|
608
|
+
* The resolved value
|
|
609
|
+
* @return {Awaitable<Next>}
|
|
610
|
+
* The next awaitable
|
|
611
|
+
*/
|
|
612
|
+
type OnFulfilled<T, Next = T> = (this: unknown, value: T) => Awaitable<Next>;
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* @file Type Aliases - OnRejected
|
|
616
|
+
* @module when/types/OnRejected
|
|
617
|
+
*/
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* The callback to execute when a `Thenable` is rejected.
|
|
621
|
+
*
|
|
622
|
+
* @see {@linkcode Awaitable}
|
|
623
|
+
*
|
|
624
|
+
* @template {any} Next
|
|
625
|
+
* The next resolved value
|
|
626
|
+
* @template {any} [Reason=any]
|
|
627
|
+
* The reason for the rejection
|
|
628
|
+
*
|
|
629
|
+
* @this {unknown}
|
|
630
|
+
*
|
|
631
|
+
* @param {Reason} reason
|
|
632
|
+
* The reason for the rejection
|
|
633
|
+
* @return {Awaitable<Next>}
|
|
634
|
+
* The next awaitable
|
|
635
|
+
*/
|
|
636
|
+
type OnRejected<Next, Reason = any> = (this: unknown, reason: Reason) => Awaitable<Next>;
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* @file Type Aliases - Then
|
|
640
|
+
* @module when/types/Then
|
|
641
|
+
*/
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Attach callbacks for the resolution and/or rejection of a `Thenable`.
|
|
645
|
+
*
|
|
646
|
+
* @see {@linkcode OnFulfilled}
|
|
647
|
+
* @see {@linkcode OnRejected}
|
|
648
|
+
* @see {@linkcode Thenable}
|
|
649
|
+
*
|
|
650
|
+
* @template {any} [T=unknown]
|
|
651
|
+
* The previously resolved value
|
|
652
|
+
* @template {any} [Reason=any]
|
|
653
|
+
* The reason for a rejection
|
|
654
|
+
* @template {any} [Succ=T]
|
|
655
|
+
* The next resolved value on success
|
|
656
|
+
* @template {any} [Fail=never]
|
|
657
|
+
* The next resolved value on failure
|
|
658
|
+
*
|
|
659
|
+
* @this {any}
|
|
660
|
+
*
|
|
661
|
+
* @param {OnFulfilled<T, Succ> | null | undefined} [onfulfilled]
|
|
662
|
+
* The callback to execute when the thenable is resolved
|
|
663
|
+
* @param {OnRejected<Fail, Reason> | null | undefined} [onrejected]
|
|
664
|
+
* The callback to execute when the thenable is rejected
|
|
665
|
+
* @return {Thenable<Fail | Succ>}
|
|
666
|
+
* The next thenable
|
|
192
667
|
*/
|
|
193
|
-
type
|
|
668
|
+
type Then<T = unknown, Reason = any> = <Succ = T, Fail = never>(this: any, onfulfilled?: OnFulfilled<T, Succ> | null | undefined, onrejected?: OnRejected<Fail, Reason> | null | undefined) => Thenable<Fail | Succ>;
|
|
194
669
|
|
|
195
|
-
export { when as default,
|
|
196
|
-
export type { Awaitable, Chain, Options,
|
|
670
|
+
export { when as default, isCatchable, isFinalizable, isPromise, isPromiseLike, isThenable, when };
|
|
671
|
+
export type { Awaitable, Catch, Catchable, Chain, Fail, Finalizable, Finally, Finish, OnFinally, OnFulfilled, OnRejected, Options, Then, Thenable };
|