@xylabs/promise 4.13.20 → 4.13.22

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/README.md CHANGED
@@ -12,13 +12,586 @@
12
12
  [![snyk-badge][]][snyk-link]
13
13
  [![socket-badge][]][socket-link]
14
14
 
15
- Version: 4.13.19
16
15
 
17
16
  Base functionality used throughout XY Labs TypeScript/JavaScript libraries
18
17
 
19
- ## Documentation
18
+ ## Reference
19
+
20
+ **@xylabs/promise**
21
+
22
+ ***
23
+
24
+ ## Classes
25
+
26
+ - [PromiseEx](#classes/PromiseEx)
27
+
28
+ ## Interfaces
29
+
30
+ - [PromiseType](#interfaces/PromiseType)
31
+
32
+ ## Type Aliases
33
+
34
+ - [PromiseExSubFunc](#type-aliases/PromiseExSubFunc)
35
+ - [PromiseExFunc](#type-aliases/PromiseExFunc)
36
+ - [PromiseExValueFunc](#type-aliases/PromiseExValueFunc)
37
+ - [AnyNonPromise](#type-aliases/AnyNonPromise)
38
+ - [Promisable](#type-aliases/Promisable)
39
+ - [PromisableArray](#type-aliases/PromisableArray)
40
+ - [OptionalPromisable](#type-aliases/OptionalPromisable)
41
+ - [OptionalPromisableArray](#type-aliases/OptionalPromisableArray)
42
+ - [NullablePromisable](#type-aliases/NullablePromisable)
43
+ - [NullablePromisableArray](#type-aliases/NullablePromisableArray)
44
+ - [AsyncMutex](#type-aliases/AsyncMutex)
45
+
46
+ ## Functions
47
+
48
+ - [fulfilled](#functions/fulfilled)
49
+ - [fulfilledValues](#functions/fulfilledValues)
50
+ - [isPromise](#functions/isPromise)
51
+ - [rejected](#functions/rejected)
52
+ - [toPromise](#functions/toPromise)
53
+
54
+ ### classes
55
+
56
+ ### <a id="PromiseEx"></a>PromiseEx
57
+
58
+ [**@xylabs/promise**](#../README)
59
+
60
+ ***
61
+
62
+ ## Extends
63
+
64
+ - `Promise`\<`T`\>
65
+
66
+ ## Type Parameters
67
+
68
+ ### T
69
+
70
+ `T`
71
+
72
+ ### V
73
+
74
+ `V` = `void`
75
+
76
+ ## Constructors
77
+
78
+ ### Constructor
79
+
80
+ ```ts
81
+ new PromiseEx<T, V>(func, value?): PromiseEx<T, V>;
82
+ ```
83
+
84
+ ### Parameters
85
+
86
+ #### func
87
+
88
+ [`PromiseExFunc`](#../type-aliases/PromiseExFunc)\<`T`\>
89
+
90
+ #### value?
91
+
92
+ `V`
93
+
94
+ ### Returns
95
+
96
+ `PromiseEx`\<`T`, `V`\>
97
+
98
+ ### Overrides
99
+
100
+ ```ts
101
+ Promise<T>.constructor
102
+ ```
103
+
104
+ ## Properties
105
+
106
+ ### cancelled?
107
+
108
+ ```ts
109
+ optional cancelled: boolean;
110
+ ```
111
+
112
+ ## Methods
113
+
114
+ ### then()
115
+
116
+ ```ts
117
+ then<TResult1, TResult2>(
118
+ onfulfilled?,
119
+ onrejected?,
120
+ onvalue?): Promise<TResult1 | TResult2>;
121
+ ```
122
+
123
+ Attaches callbacks for the resolution and/or rejection of the Promise.
124
+
125
+ ### Type Parameters
126
+
127
+ #### TResult1
128
+
129
+ `TResult1` = `T`
130
+
131
+ #### TResult2
132
+
133
+ `TResult2` = `never`
134
+
135
+ ### Parameters
136
+
137
+ #### onfulfilled?
138
+
139
+ The callback to execute when the Promise is resolved.
140
+
141
+ `null` | (`value`) => `TResult1` \| `PromiseLike`\<`TResult1`\>
142
+
143
+ #### onrejected?
144
+
145
+ The callback to execute when the Promise is rejected.
146
+
147
+ `null` | (`reason`) => `TResult2` \| `PromiseLike`\<`TResult2`\>
148
+
149
+ #### onvalue?
150
+
151
+ (`value?`) => `boolean`
152
+
153
+ ### Returns
154
+
155
+ `Promise`\<`TResult1` \| `TResult2`\>
156
+
157
+ A Promise for the completion of which ever callback is executed.
158
+
159
+ ### Overrides
160
+
161
+ ```ts
162
+ Promise.then
163
+ ```
164
+
165
+ ***
166
+
167
+ ### value()
168
+
169
+ ```ts
170
+ value(onvalue?): PromiseEx<T, V>;
171
+ ```
172
+
173
+ ### Parameters
174
+
175
+ #### onvalue?
176
+
177
+ (`value?`) => `boolean`
178
+
179
+ ### Returns
180
+
181
+ `PromiseEx`\<`T`, `V`\>
182
+
183
+ ### functions
184
+
185
+ ### <a id="fulfilled"></a>fulfilled
186
+
187
+ [**@xylabs/promise**](#../README)
188
+
189
+ ***
190
+
191
+ ```ts
192
+ function fulfilled<T>(val): val is PromiseFulfilledResult<T>;
193
+ ```
194
+
195
+ For use with Promise.allSettled to filter only successful results
196
+
197
+ ## Type Parameters
198
+
199
+ ### T
200
+
201
+ `T`
202
+
203
+ ## Parameters
204
+
205
+ ### val
206
+
207
+ `PromiseSettledResult`\<`T`\>
208
+
209
+ ## Returns
210
+
211
+ `val is PromiseFulfilledResult<T>`
212
+
213
+ ### <a id="fulfilledValues"></a>fulfilledValues
214
+
215
+ [**@xylabs/promise**](#../README)
216
+
217
+ ***
218
+
219
+ ```ts
220
+ function fulfilledValues<T>(previousValue, currentValue): T[];
221
+ ```
222
+
223
+ For use with Promise.allSettled to reduce to only successful result values
224
+
225
+ ## Type Parameters
226
+
227
+ ### T
228
+
229
+ `T`
230
+
231
+ ## Parameters
232
+
233
+ ### previousValue
234
+
235
+ `T`[]
236
+
237
+ ### currentValue
238
+
239
+ `PromiseSettledResult`\<`T`\>
240
+
241
+ ## Returns
242
+
243
+ `T`[]
244
+
245
+ ## Examples
246
+
247
+ ```ts
248
+ const resolved = Promise.resolve('resolved')
249
+ const rejected = Promise.reject('rejected')
250
+ const settled = await Promise.allSettled([resolved, rejected])
251
+ const results = settled.reduce(fulfilledValues, [] as string[])
252
+ // results === [ 'resolved' ]
253
+ ```
254
+
255
+ ```ts
256
+ const resolved = Promise.resolve('resolved')
257
+ const rejected = Promise.reject('rejected')
258
+ const settled = await Promise.allSettled([resolved, rejected])
259
+ const results = settled.reduce<string[]>(fulfilledValues, [])
260
+ // results === [ 'resolved' ]
261
+ ```
262
+
263
+ ### <a id="isPromise"></a>isPromise
264
+
265
+ [**@xylabs/promise**](#../README)
266
+
267
+ ***
268
+
269
+ ```ts
270
+ function isPromise(value): value is Promise<unknown>;
271
+ ```
272
+
273
+ ## Parameters
274
+
275
+ ### value
276
+
277
+ `unknown`
278
+
279
+ ## Returns
280
+
281
+ `value is Promise<unknown>`
282
+
283
+ ### <a id="rejected"></a>rejected
284
+
285
+ [**@xylabs/promise**](#../README)
286
+
287
+ ***
288
+
289
+ ```ts
290
+ function rejected<T>(val): val is PromiseRejectedResult;
291
+ ```
292
+
293
+ For use with Promise.allSettled to filter only rejected results
294
+
295
+ ## Type Parameters
296
+
297
+ ### T
298
+
299
+ `T`
300
+
301
+ ## Parameters
302
+
303
+ ### val
304
+
305
+ `PromiseSettledResult`\<`T`\>
306
+
307
+ ## Returns
308
+
309
+ `val is PromiseRejectedResult`
310
+
311
+ ### <a id="toPromise"></a>toPromise
312
+
313
+ [**@xylabs/promise**](#../README)
314
+
315
+ ***
316
+
317
+ ```ts
318
+ function toPromise<T>(value): Promise<T>;
319
+ ```
320
+
321
+ ## Type Parameters
322
+
323
+ ### T
324
+
325
+ `T`
326
+
327
+ ## Parameters
328
+
329
+ ### value
330
+
331
+ [`Promisable`](#../type-aliases/Promisable)\<`T`\>
332
+
333
+ ## Returns
334
+
335
+ `Promise`\<`T`\>
336
+
337
+ ### interfaces
338
+
339
+ ### <a id="PromiseType"></a>PromiseType
340
+
341
+ [**@xylabs/promise**](#../README)
342
+
343
+ ***
344
+
345
+ ## Properties
346
+
347
+ ### then()
348
+
349
+ ```ts
350
+ then: () => unknown;
351
+ ```
352
+
353
+ ### Returns
354
+
355
+ `unknown`
356
+
357
+ ### type-aliases
358
+
359
+ ### <a id="AnyNonPromise"></a>AnyNonPromise
360
+
361
+ [**@xylabs/promise**](#../README)
362
+
363
+ ***
364
+
365
+ ```ts
366
+ type AnyNonPromise = Exclude<TypedValue, PromiseType>;
367
+ ```
368
+
369
+ ### <a id="AsyncMutex"></a>AsyncMutex
370
+
371
+ [**@xylabs/promise**](#../README)
372
+
373
+ ***
374
+
375
+ ```ts
376
+ type AsyncMutex<T> = Promise<T>;
377
+ ```
378
+
379
+ ## Type Parameters
380
+
381
+ ### T
382
+
383
+ `T`
384
+
385
+ ## Description
386
+
387
+ Used to document promises that are being used as Mutexes
388
+
389
+ ### <a id="NullablePromisable"></a>NullablePromisable
390
+
391
+ [**@xylabs/promise**](#../README)
392
+
393
+ ***
394
+
395
+ ```ts
396
+ type NullablePromisable<T, V> = Promisable<T | null, V>;
397
+ ```
398
+
399
+ ## Type Parameters
400
+
401
+ ### T
402
+
403
+ `T`
404
+
405
+ ### V
406
+
407
+ `V` = `never`
408
+
409
+ ### <a id="NullablePromisableArray"></a>NullablePromisableArray
410
+
411
+ [**@xylabs/promise**](#../README)
412
+
413
+ ***
414
+
415
+ ```ts
416
+ type NullablePromisableArray<T, V> = PromisableArray<T | null, V>;
417
+ ```
418
+
419
+ ## Type Parameters
420
+
421
+ ### T
422
+
423
+ `T`
424
+
425
+ ### V
426
+
427
+ `V` = `never`
428
+
429
+ ### <a id="OptionalPromisable"></a>OptionalPromisable
430
+
431
+ [**@xylabs/promise**](#../README)
432
+
433
+ ***
434
+
435
+ ```ts
436
+ type OptionalPromisable<T, V> = Promisable<T | undefined, V>;
437
+ ```
438
+
439
+ ## Type Parameters
440
+
441
+ ### T
442
+
443
+ `T`
444
+
445
+ ### V
446
+
447
+ `V` = `never`
448
+
449
+ ### <a id="OptionalPromisableArray"></a>OptionalPromisableArray
450
+
451
+ [**@xylabs/promise**](#../README)
452
+
453
+ ***
454
+
455
+ ```ts
456
+ type OptionalPromisableArray<T, V> = PromisableArray<T | undefined, V>;
457
+ ```
458
+
459
+ ## Type Parameters
460
+
461
+ ### T
462
+
463
+ `T`
464
+
465
+ ### V
466
+
467
+ `V` = `never`
468
+
469
+ ### <a id="Promisable"></a>Promisable
470
+
471
+ [**@xylabs/promise**](#../README)
472
+
473
+ ***
474
+
475
+ ```ts
476
+ type Promisable<T, V> = PromiseEx<T, V> | Promise<T> | T;
477
+ ```
478
+
479
+ ## Type Parameters
480
+
481
+ ### T
482
+
483
+ `T`
484
+
485
+ ### V
486
+
487
+ `V` = `never`
488
+
489
+ ### <a id="PromisableArray"></a>PromisableArray
490
+
491
+ [**@xylabs/promise**](#../README)
492
+
493
+ ***
494
+
495
+ ```ts
496
+ type PromisableArray<T, V> = Promisable<T[], V>;
497
+ ```
498
+
499
+ ## Type Parameters
500
+
501
+ ### T
502
+
503
+ `T`
504
+
505
+ ### V
506
+
507
+ `V` = `never`
508
+
509
+ ### <a id="PromiseExFunc"></a>PromiseExFunc
510
+
511
+ [**@xylabs/promise**](#../README)
512
+
513
+ ***
514
+
515
+ ```ts
516
+ type PromiseExFunc<T> = (resolve?, reject?) => void;
517
+ ```
518
+
519
+ ## Type Parameters
520
+
521
+ ### T
522
+
523
+ `T`
524
+
525
+ ## Parameters
526
+
527
+ ### resolve?
528
+
529
+ [`PromiseExSubFunc`](#PromiseExSubFunc)\<`T`, `void`\>
530
+
531
+ ### reject?
532
+
533
+ [`PromiseExSubFunc`](#PromiseExSubFunc)\<`T`, `void`\>
534
+
535
+ ## Returns
536
+
537
+ `void`
538
+
539
+ ### <a id="PromiseExSubFunc"></a>PromiseExSubFunc
540
+
541
+ [**@xylabs/promise**](#../README)
542
+
543
+ ***
544
+
545
+ ```ts
546
+ type PromiseExSubFunc<T, TResult> = (value) => TResult;
547
+ ```
548
+
549
+ ## Type Parameters
550
+
551
+ ### T
552
+
553
+ `T`
554
+
555
+ ### TResult
556
+
557
+ `TResult` = `T`
558
+
559
+ ## Parameters
560
+
561
+ ### value
562
+
563
+ `T`
564
+
565
+ ## Returns
566
+
567
+ `TResult`
568
+
569
+ ### <a id="PromiseExValueFunc"></a>PromiseExValueFunc
570
+
571
+ [**@xylabs/promise**](#../README)
572
+
573
+ ***
574
+
575
+ ```ts
576
+ type PromiseExValueFunc<V> = (value?) => boolean;
577
+ ```
578
+
579
+ ## Type Parameters
580
+
581
+ ### V
582
+
583
+ `V`
584
+
585
+ ## Parameters
586
+
587
+ ### value?
588
+
589
+ `V`
590
+
591
+ ## Returns
592
+
593
+ `boolean`
20
594
 
21
- Coming Soon!
22
595
 
23
596
  Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
24
597
 
@@ -1,8 +1,66 @@
1
- export { fulfilled } from './fulfilled.ts';
2
- export { fulfilledValues } from './fulfilledValues.ts';
3
- export * from './isPromise.ts';
4
- export * from './PromiseEx.ts';
5
- export { rejected } from './rejected.ts';
6
- export * from './toPromise.ts';
7
- export * from './types.ts';
8
- //# sourceMappingURL=index.d.ts.map
1
+ import { TypedValue } from '@xylabs/typeof';
2
+
3
+ /**
4
+ * For use with Promise.allSettled to filter only successful results
5
+ * @param val
6
+ * @returns
7
+ */
8
+ declare const fulfilled: <T>(val: PromiseSettledResult<T>) => val is PromiseFulfilledResult<T>;
9
+
10
+ /**
11
+ * For use with Promise.allSettled to reduce to only successful result values
12
+ * @example <caption>Casting the initialValue provided to reduce</caption>
13
+ * const resolved = Promise.resolve('resolved')
14
+ * const rejected = Promise.reject('rejected')
15
+ * const settled = await Promise.allSettled([resolved, rejected])
16
+ * const results = settled.reduce(fulfilledValues, [] as string[])
17
+ * // results === [ 'resolved' ]
18
+ * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>
19
+ * const resolved = Promise.resolve('resolved')
20
+ * const rejected = Promise.reject('rejected')
21
+ * const settled = await Promise.allSettled([resolved, rejected])
22
+ * const results = settled.reduce<string[]>(fulfilledValues, [])
23
+ * // results === [ 'resolved' ]
24
+ * @param previousValue
25
+ * @param currentValue
26
+ * @returns
27
+ */
28
+ declare const fulfilledValues: <T>(previousValue: T[], currentValue: PromiseSettledResult<T>) => T[];
29
+
30
+ interface PromiseType {
31
+ then: () => unknown;
32
+ }
33
+ type AnyNonPromise = Exclude<TypedValue, PromiseType>;
34
+ declare const isPromise: (value: unknown) => value is Promise<unknown>;
35
+
36
+ type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult;
37
+ type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void;
38
+ type PromiseExValueFunc<V> = (value?: V) => boolean;
39
+ declare class PromiseEx<T, V = void> extends Promise<T> {
40
+ cancelled?: boolean;
41
+ private _value?;
42
+ constructor(func: PromiseExFunc<T>, value?: V);
43
+ then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined, onvalue?: (value?: V) => boolean): Promise<TResult1 | TResult2>;
44
+ value(onvalue?: (value?: V) => boolean): this;
45
+ }
46
+
47
+ /**
48
+ * For use with Promise.allSettled to filter only rejected results
49
+ * @param val
50
+ * @returns
51
+ */
52
+ declare const rejected: <T>(val: PromiseSettledResult<T>) => val is PromiseRejectedResult;
53
+
54
+ type Promisable<T, V = never> = PromiseEx<T, V> | Promise<T> | T;
55
+ type PromisableArray<T, V = never> = Promisable<T[], V>;
56
+ type OptionalPromisable<T, V = never> = Promisable<T | undefined, V>;
57
+ type OptionalPromisableArray<T, V = never> = PromisableArray<T | undefined, V>;
58
+ type NullablePromisable<T, V = never> = Promisable<T | null, V>;
59
+ type NullablePromisableArray<T, V = never> = PromisableArray<T | null, V>;
60
+ /** @description Used to document promises that are being used as Mutexes */
61
+ type AsyncMutex<T> = Promise<T>;
62
+
63
+ declare function toPromise<T>(value: Promisable<T>): Promise<T>;
64
+
65
+ export { PromiseEx, fulfilled, fulfilledValues, isPromise, rejected, toPromise };
66
+ export type { AnyNonPromise, AsyncMutex, NullablePromisable, NullablePromisableArray, OptionalPromisable, OptionalPromisableArray, Promisable, PromisableArray, PromiseExFunc, PromiseExSubFunc, PromiseExValueFunc, PromiseType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/promise",
3
- "version": "4.13.20",
3
+ "version": "4.13.22",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "promise",
@@ -41,11 +41,13 @@
41
41
  "dist",
42
42
  "src"
43
43
  ],
44
+ "dependencies": {
45
+ "@xylabs/typeof": "^4.13.22"
46
+ },
44
47
  "devDependencies": {
45
48
  "@xylabs/ts-scripts-yarn3": "^7.0.0",
46
49
  "@xylabs/tsconfig": "^7.0.0",
47
- "@xylabs/typeof": "^4.13.20",
48
- "@xylabs/vitest-extended": "^4.13.20",
50
+ "@xylabs/vitest-extended": "^4.13.22",
49
51
  "typescript": "^5.8.3",
50
52
  "vitest": "^3.2.4"
51
53
  },