@xylabs/promise 5.0.83 → 5.0.84

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
@@ -59,6 +59,9 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
59
59
 
60
60
  ***
61
61
 
62
+ An extended Promise that carries an optional attached value and supports cancellation.
63
+ The value can be inspected via the `then` or `value` methods to conditionally cancel.
64
+
62
65
  ## Extends
63
66
 
64
67
  - `Promise`\<`T`\>
@@ -109,6 +112,8 @@ Promise<T>.constructor
109
112
  optional cancelled: boolean;
110
113
  ```
111
114
 
115
+ Whether the promise has been cancelled via a value callback.
116
+
112
117
  ## Methods
113
118
 
114
119
  ### then()
@@ -170,16 +175,22 @@ Promise.then
170
175
  value(onvalue?): PromiseEx<T, V>;
171
176
  ```
172
177
 
178
+ Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.
179
+
173
180
  ### Parameters
174
181
 
175
182
  #### onvalue?
176
183
 
177
184
  (`value?`) => `boolean`
178
185
 
186
+ A callback that receives the attached value and returns whether to cancel.
187
+
179
188
  ### Returns
180
189
 
181
190
  `PromiseEx`\<`T`, `V`\>
182
191
 
192
+ This instance for chaining.
193
+
183
194
  ### functions
184
195
 
185
196
  ### <a id="fulfilled"></a>fulfilled
@@ -272,6 +283,8 @@ const results = settled.reduce<string[]>(fulfilledValues, [])
272
283
  function isPromise(value): value is Promise<unknown>;
273
284
  ```
274
285
 
286
+ Type guard that checks whether a value is a Promise instance.
287
+
275
288
  ### Parameters
276
289
 
277
290
  ### value
@@ -288,6 +301,8 @@ function isPromise(value): value is Promise<unknown>;
288
301
  function isPromise<T>(value): value is Extract<T, Promise<unknown>>;
289
302
  ```
290
303
 
304
+ Type guard that checks whether a value is a Promise instance.
305
+
291
306
  ### Type Parameters
292
307
 
293
308
  ### T
@@ -342,6 +357,8 @@ For use with Promise.allSettled to filter only rejected results
342
357
  function toPromise<T>(value): Promise<T>;
343
358
  ```
344
359
 
360
+ Wraps a value in a Promise if it is not already one.
361
+
345
362
  ## Type Parameters
346
363
 
347
364
  ### T
@@ -354,10 +371,14 @@ function toPromise<T>(value): Promise<T>;
354
371
 
355
372
  [`Promisable`](#../type-aliases/Promisable)\<`T`\>
356
373
 
374
+ A value that may or may not be a Promise.
375
+
357
376
  ## Returns
358
377
 
359
378
  `Promise`\<`T`\>
360
379
 
380
+ A Promise resolving to the value.
381
+
361
382
  ### interfaces
362
383
 
363
384
  ### <a id="PromiseType"></a>PromiseType
@@ -366,6 +387,8 @@ function toPromise<T>(value): Promise<T>;
366
387
 
367
388
  ***
368
389
 
390
+ An interface representing any thenable (promise-like) object.
391
+
369
392
  ## Properties
370
393
 
371
394
  ### then()
@@ -390,6 +413,8 @@ then: () => unknown;
390
413
  type AnyNonPromise = Exclude<TypedValue, PromiseType>;
391
414
  ```
392
415
 
416
+ Any non-promise typed value, excluding thenables.
417
+
393
418
  ### <a id="AsyncMutex"></a>AsyncMutex
394
419
 
395
420
  [**@xylabs/promise**](#../README)
@@ -420,6 +445,8 @@ Used to document promises that are being used as Mutexes
420
445
  type NullablePromisable<T, V> = Promisable<T | null, V>;
421
446
  ```
422
447
 
448
+ A Promisable that may resolve to null.
449
+
423
450
  ## Type Parameters
424
451
 
425
452
  ### T
@@ -440,6 +467,8 @@ type NullablePromisable<T, V> = Promisable<T | null, V>;
440
467
  type NullablePromisableArray<T, V> = PromisableArray<T | null, V>;
441
468
  ```
442
469
 
470
+ A Promisable array where elements may be null.
471
+
443
472
  ## Type Parameters
444
473
 
445
474
  ### T
@@ -460,6 +489,8 @@ type NullablePromisableArray<T, V> = PromisableArray<T | null, V>;
460
489
  type OptionalPromisable<T, V> = Promisable<T | undefined, V>;
461
490
  ```
462
491
 
492
+ A Promisable that may resolve to undefined.
493
+
463
494
  ## Type Parameters
464
495
 
465
496
  ### T
@@ -480,6 +511,8 @@ type OptionalPromisable<T, V> = Promisable<T | undefined, V>;
480
511
  type OptionalPromisableArray<T, V> = PromisableArray<T | undefined, V>;
481
512
  ```
482
513
 
514
+ A Promisable array where elements may be undefined.
515
+
483
516
  ## Type Parameters
484
517
 
485
518
  ### T
@@ -500,6 +533,8 @@ type OptionalPromisableArray<T, V> = PromisableArray<T | undefined, V>;
500
533
  type Promisable<T, V> = PromiseEx<T, V> | Promise<T> | T;
501
534
  ```
502
535
 
536
+ A value that may be a Promise, PromiseEx, or a plain synchronous value.
537
+
503
538
  ## Type Parameters
504
539
 
505
540
  ### T
@@ -520,6 +555,8 @@ type Promisable<T, V> = PromiseEx<T, V> | Promise<T> | T;
520
555
  type PromisableArray<T, V> = Promisable<T[], V>;
521
556
  ```
522
557
 
558
+ A Promisable that resolves to an array.
559
+
523
560
  ## Type Parameters
524
561
 
525
562
  ### T
@@ -540,6 +577,8 @@ type PromisableArray<T, V> = Promisable<T[], V>;
540
577
  type PromiseExFunc<T> = (resolve?, reject?) => void;
541
578
  ```
542
579
 
580
+ The executor function passed to the PromiseEx constructor.
581
+
543
582
  ## Type Parameters
544
583
 
545
584
  ### T
@@ -570,6 +609,8 @@ type PromiseExFunc<T> = (resolve?, reject?) => void;
570
609
  type PromiseExSubFunc<T, TResult> = (value) => TResult;
571
610
  ```
572
611
 
612
+ A resolve/reject callback used within PromiseEx.
613
+
573
614
  ## Type Parameters
574
615
 
575
616
  ### T
@@ -600,6 +641,8 @@ type PromiseExSubFunc<T, TResult> = (value) => TResult;
600
641
  type PromiseExValueFunc<V> = (value?) => boolean;
601
642
  ```
602
643
 
644
+ A callback that inspects the attached value and returns whether to cancel the promise.
645
+
603
646
  ## Type Parameters
604
647
 
605
648
  ### V
@@ -1,11 +1,24 @@
1
+ /** A resolve/reject callback used within PromiseEx. */
1
2
  export type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult;
3
+ /** The executor function passed to the PromiseEx constructor. */
2
4
  export type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void;
5
+ /** A callback that inspects the attached value and returns whether to cancel the promise. */
3
6
  export type PromiseExValueFunc<V> = (value?: V) => boolean;
7
+ /**
8
+ * An extended Promise that carries an optional attached value and supports cancellation.
9
+ * The value can be inspected via the `then` or `value` methods to conditionally cancel.
10
+ */
4
11
  export declare class PromiseEx<T, V = void> extends Promise<T> {
12
+ /** Whether the promise has been cancelled via a value callback. */
5
13
  cancelled?: boolean;
6
14
  private _value?;
7
15
  constructor(func: PromiseExFunc<T>, value?: V);
8
16
  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>;
17
+ /**
18
+ * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.
19
+ * @param onvalue - A callback that receives the attached value and returns whether to cancel.
20
+ * @returns This instance for chaining.
21
+ */
9
22
  value(onvalue?: (value?: V) => boolean): this;
10
23
  }
11
24
  //# sourceMappingURL=PromiseEx.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PromiseEx.d.ts","sourceRoot":"","sources":["../../src/PromiseEx.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAA;AACpE,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,IAAI,CAAA;AAChH,MAAM,MAAM,kBAAkB,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,OAAO,CAAA;AAE1D,qBAAa,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAC;IACpD,S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,GAAG,IAAI,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAC;IACpD,mEAAmE;IACnE,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,OAAO,CAAC,MAAM,CAAC,CAAG;gBAEN,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC;IAMpC,IAAI,CAAC,QAAQ,GAAG,CAAC,EAAE,QAAQ,GAAG,KAAK,EAC1C,WAAW,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EACjF,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,OAAO,KAAK,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EACvF,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,OAAO,GAC/B,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAO/B;;;;OAIG;IACH,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,OAAO;CAMvC"}
@@ -28,14 +28,27 @@ declare const fulfilled: <T>(val: PromiseSettledResult<T>) => val is PromiseFulf
28
28
  */
29
29
  declare const fulfilledValues: <T>(previousValue: T[], currentValue: PromiseSettledResult<T>) => T[];
30
30
 
31
+ /** A resolve/reject callback used within PromiseEx. */
31
32
  type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult;
33
+ /** The executor function passed to the PromiseEx constructor. */
32
34
  type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void;
35
+ /** A callback that inspects the attached value and returns whether to cancel the promise. */
33
36
  type PromiseExValueFunc<V> = (value?: V) => boolean;
37
+ /**
38
+ * An extended Promise that carries an optional attached value and supports cancellation.
39
+ * The value can be inspected via the `then` or `value` methods to conditionally cancel.
40
+ */
34
41
  declare class PromiseEx<T, V = void> extends Promise<T> {
42
+ /** Whether the promise has been cancelled via a value callback. */
35
43
  cancelled?: boolean;
36
44
  private _value?;
37
45
  constructor(func: PromiseExFunc<T>, value?: V);
38
46
  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>;
47
+ /**
48
+ * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.
49
+ * @param onvalue - A callback that receives the attached value and returns whether to cancel.
50
+ * @returns This instance for chaining.
51
+ */
39
52
  value(onvalue?: (value?: V) => boolean): this;
40
53
  }
41
54
 
@@ -46,19 +59,32 @@ declare class PromiseEx<T, V = void> extends Promise<T> {
46
59
  */
47
60
  declare const rejected: <T>(val: PromiseSettledResult<T>) => val is PromiseRejectedResult;
48
61
 
62
+ /** A value that may be a Promise, PromiseEx, or a plain synchronous value. */
49
63
  type Promisable<T, V = never> = PromiseEx<T, V> | Promise<T> | T;
64
+ /** A Promisable that resolves to an array. */
50
65
  type PromisableArray<T, V = never> = Promisable<T[], V>;
66
+ /** A Promisable that may resolve to undefined. */
51
67
  type OptionalPromisable<T, V = never> = Promisable<T | undefined, V>;
68
+ /** A Promisable array where elements may be undefined. */
52
69
  type OptionalPromisableArray<T, V = never> = PromisableArray<T | undefined, V>;
70
+ /** A Promisable that may resolve to null. */
53
71
  type NullablePromisable<T, V = never> = Promisable<T | null, V>;
72
+ /** A Promisable array where elements may be null. */
54
73
  type NullablePromisableArray<T, V = never> = PromisableArray<T | null, V>;
55
74
  /** @description Used to document promises that are being used as Mutexes */
56
75
  type AsyncMutex<T> = Promise<T>;
76
+ /** An interface representing any thenable (promise-like) object. */
57
77
  interface PromiseType {
58
78
  then: () => unknown;
59
79
  }
80
+ /** Any non-promise typed value, excluding thenables. */
60
81
  type AnyNonPromise = Exclude<TypedValue, PromiseType>;
61
82
 
83
+ /**
84
+ * Wraps a value in a Promise if it is not already one.
85
+ * @param value - A value that may or may not be a Promise.
86
+ * @returns A Promise resolving to the value.
87
+ */
62
88
  declare function toPromise<T>(value: Promisable<T>): Promise<T>;
63
89
 
64
90
  export { PromiseEx, fulfilled, fulfilledValues, rejected, toPromise };
@@ -11,6 +11,7 @@ var fulfilledValues = (previousValue, currentValue) => {
11
11
 
12
12
  // src/PromiseEx.ts
13
13
  var PromiseEx = class extends Promise {
14
+ /** Whether the promise has been cancelled via a value callback. */
14
15
  cancelled;
15
16
  _value;
16
17
  constructor(func, value) {
@@ -24,6 +25,11 @@ var PromiseEx = class extends Promise {
24
25
  }
25
26
  return super.then(onfulfilled, onrejected);
26
27
  }
28
+ /**
29
+ * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.
30
+ * @param onvalue - A callback that receives the attached value and returns whether to cancel.
31
+ * @returns This instance for chaining.
32
+ */
27
33
  value(onvalue) {
28
34
  if (onvalue?.(this._value)) {
29
35
  this.cancelled = true;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/toPromise.ts","../../src/index.ts"],"sourcesContent":["/**\n * For use with Promise.allSettled to filter only successful results\n * @param val\n * @returns\n */\nexport const fulfilled = <T>(val: PromiseSettledResult<T>): val is PromiseFulfilledResult<T> => {\n return val.status === 'fulfilled'\n}\n","/**\n * For use with Promise.allSettled to reduce to only successful result values\n * @example <caption>Casting the initialValue provided to reduce</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce(fulfilledValues, [] as string[])\n * // results === [ 'resolved' ]\n * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce<string[]>(fulfilledValues, [])\n * // results === [ 'resolved' ]\n * @param previousValue\n * @param currentValue\n * @returns\n */\nexport const fulfilledValues = <T>(previousValue: T[], currentValue: PromiseSettledResult<T>): T[] => {\n if (currentValue.status === 'fulfilled') previousValue.push(currentValue.value)\n return previousValue\n}\n","export type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult\nexport type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void\nexport type PromiseExValueFunc<V> = (value?: V) => boolean\n\nexport class PromiseEx<T, V = void> extends Promise<T> {\n cancelled?: boolean\n private _value?: V\n\n constructor(func: PromiseExFunc<T>, value?: V) {\n super(func)\n this._value = value\n }\n\n // eslint-disable-next-line unicorn/no-thenable\n override then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n onvalue?: (value?: V) => boolean,\n ): Promise<TResult1 | TResult2> {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return super.then(onfulfilled, onrejected)\n }\n\n value(onvalue?: (value?: V) => boolean) {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return this\n }\n}\n","/**\n * For use with Promise.allSettled to filter only rejected results\n * @param val\n * @returns\n */\nexport const rejected = <T>(val: PromiseSettledResult<T>): val is PromiseRejectedResult => {\n return val.status === 'rejected'\n}\n","import type { Promisable } from './types.ts'\n\nexport function toPromise<T>(value: Promisable<T>): Promise<T> {\n return value instanceof Promise ? value : Promise.resolve(value)\n}\n","export { fulfilled } from './fulfilled.ts'\nexport { fulfilledValues } from './fulfilledValues.ts'\nexport * from './PromiseEx.ts'\nexport { rejected } from './rejected.ts'\nexport * from './toPromise.ts'\nexport * from './types.ts'\nexport { isPromise } from '@xylabs/typeof'\n"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW,YAAa,eAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACjBO,IAAM,YAAN,cAAqC,QAAW;AAAA,EACrD;AAAA,EACQ;AAAA,EAER,YAAY,MAAwB,OAAW;AAC7C,UAAM,IAAI;AACV,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA,EAGS,KACP,aACA,YACA,SAC8B;AAC9B,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA,EAEA,MAAM,SAAkC;AACtC,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;AC1BO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACLO,SAAS,UAAa,OAAkC;AAC7D,SAAO,iBAAiB,UAAU,QAAQ,QAAQ,QAAQ,KAAK;AACjE;;;ACEA,SAAS,iBAAiB;","names":[]}
1
+ {"version":3,"sources":["../../src/fulfilled.ts","../../src/fulfilledValues.ts","../../src/PromiseEx.ts","../../src/rejected.ts","../../src/toPromise.ts","../../src/index.ts"],"sourcesContent":["/**\n * For use with Promise.allSettled to filter only successful results\n * @param val\n * @returns\n */\nexport const fulfilled = <T>(val: PromiseSettledResult<T>): val is PromiseFulfilledResult<T> => {\n return val.status === 'fulfilled'\n}\n","/**\n * For use with Promise.allSettled to reduce to only successful result values\n * @example <caption>Casting the initialValue provided to reduce</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce(fulfilledValues, [] as string[])\n * // results === [ 'resolved' ]\n * @example <caption>Providing type parameter to reduce and initialValue type can be inferred</caption>\n * const resolved = Promise.resolve('resolved')\n * const rejected = Promise.reject('rejected')\n * const settled = await Promise.allSettled([resolved, rejected])\n * const results = settled.reduce<string[]>(fulfilledValues, [])\n * // results === [ 'resolved' ]\n * @param previousValue\n * @param currentValue\n * @returns\n */\nexport const fulfilledValues = <T>(previousValue: T[], currentValue: PromiseSettledResult<T>): T[] => {\n if (currentValue.status === 'fulfilled') previousValue.push(currentValue.value)\n return previousValue\n}\n","/** A resolve/reject callback used within PromiseEx. */\nexport type PromiseExSubFunc<T, TResult = T> = (value: T) => TResult\n\n/** The executor function passed to the PromiseEx constructor. */\nexport type PromiseExFunc<T> = (resolve?: PromiseExSubFunc<T, void>, reject?: PromiseExSubFunc<T, void>) => void\n\n/** A callback that inspects the attached value and returns whether to cancel the promise. */\nexport type PromiseExValueFunc<V> = (value?: V) => boolean\n\n/**\n * An extended Promise that carries an optional attached value and supports cancellation.\n * The value can be inspected via the `then` or `value` methods to conditionally cancel.\n */\nexport class PromiseEx<T, V = void> extends Promise<T> {\n /** Whether the promise has been cancelled via a value callback. */\n cancelled?: boolean\n private _value?: V\n\n constructor(func: PromiseExFunc<T>, value?: V) {\n super(func)\n this._value = value\n }\n\n // eslint-disable-next-line unicorn/no-thenable\n override then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined,\n onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined,\n onvalue?: (value?: V) => boolean,\n ): Promise<TResult1 | TResult2> {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return super.then(onfulfilled, onrejected)\n }\n\n /**\n * Inspects the attached value via the callback; if it returns true, marks the promise as cancelled.\n * @param onvalue - A callback that receives the attached value and returns whether to cancel.\n * @returns This instance for chaining.\n */\n value(onvalue?: (value?: V) => boolean) {\n if (onvalue?.(this._value)) {\n this.cancelled = true\n }\n return this\n }\n}\n","/**\n * For use with Promise.allSettled to filter only rejected results\n * @param val\n * @returns\n */\nexport const rejected = <T>(val: PromiseSettledResult<T>): val is PromiseRejectedResult => {\n return val.status === 'rejected'\n}\n","import type { Promisable } from './types.ts'\n\n/**\n * Wraps a value in a Promise if it is not already one.\n * @param value - A value that may or may not be a Promise.\n * @returns A Promise resolving to the value.\n */\nexport function toPromise<T>(value: Promisable<T>): Promise<T> {\n return value instanceof Promise ? value : Promise.resolve(value)\n}\n","export { fulfilled } from './fulfilled.ts'\nexport { fulfilledValues } from './fulfilledValues.ts'\nexport * from './PromiseEx.ts'\nexport { rejected } from './rejected.ts'\nexport * from './toPromise.ts'\nexport * from './types.ts'\nexport { isPromise } from '@xylabs/typeof'\n"],"mappings":";AAKO,IAAM,YAAY,CAAI,QAAmE;AAC9F,SAAO,IAAI,WAAW;AACxB;;;ACWO,IAAM,kBAAkB,CAAI,eAAoB,iBAA+C;AACpG,MAAI,aAAa,WAAW,YAAa,eAAc,KAAK,aAAa,KAAK;AAC9E,SAAO;AACT;;;ACRO,IAAM,YAAN,cAAqC,QAAW;AAAA;AAAA,EAErD;AAAA,EACQ;AAAA,EAER,YAAY,MAAwB,OAAW;AAC7C,UAAM,IAAI;AACV,SAAK,SAAS;AAAA,EAChB;AAAA;AAAA,EAGS,KACP,aACA,YACA,SAC8B;AAC9B,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO,MAAM,KAAK,aAAa,UAAU;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,SAAkC;AACtC,QAAI,UAAU,KAAK,MAAM,GAAG;AAC1B,WAAK,YAAY;AAAA,IACnB;AACA,WAAO;AAAA,EACT;AACF;;;ACzCO,IAAM,WAAW,CAAI,QAA+D;AACzF,SAAO,IAAI,WAAW;AACxB;;;ACAO,SAAS,UAAa,OAAkC;AAC7D,SAAO,iBAAiB,UAAU,QAAQ,QAAQ,QAAQ,KAAK;AACjE;;;ACHA,SAAS,iBAAiB;","names":[]}
@@ -1,3 +1,8 @@
1
1
  import type { Promisable } from './types.ts';
2
+ /**
3
+ * Wraps a value in a Promise if it is not already one.
4
+ * @param value - A value that may or may not be a Promise.
5
+ * @returns A Promise resolving to the value.
6
+ */
2
7
  export declare function toPromise<T>(value: Promisable<T>): Promise<T>;
3
8
  //# sourceMappingURL=toPromise.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"toPromise.d.ts","sourceRoot":"","sources":["../../src/toPromise.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAE7D"}
1
+ {"version":3,"file":"toPromise.d.ts","sourceRoot":"","sources":["../../src/toPromise.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAE5C;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAE7D"}
@@ -1,15 +1,23 @@
1
1
  import type { TypedValue } from '@xylabs/typeof';
2
2
  import type { PromiseEx } from './PromiseEx.ts';
3
+ /** A value that may be a Promise, PromiseEx, or a plain synchronous value. */
3
4
  export type Promisable<T, V = never> = PromiseEx<T, V> | Promise<T> | T;
5
+ /** A Promisable that resolves to an array. */
4
6
  export type PromisableArray<T, V = never> = Promisable<T[], V>;
7
+ /** A Promisable that may resolve to undefined. */
5
8
  export type OptionalPromisable<T, V = never> = Promisable<T | undefined, V>;
9
+ /** A Promisable array where elements may be undefined. */
6
10
  export type OptionalPromisableArray<T, V = never> = PromisableArray<T | undefined, V>;
11
+ /** A Promisable that may resolve to null. */
7
12
  export type NullablePromisable<T, V = never> = Promisable<T | null, V>;
13
+ /** A Promisable array where elements may be null. */
8
14
  export type NullablePromisableArray<T, V = never> = PromisableArray<T | null, V>;
9
15
  /** @description Used to document promises that are being used as Mutexes */
10
16
  export type AsyncMutex<T> = Promise<T>;
17
+ /** An interface representing any thenable (promise-like) object. */
11
18
  export interface PromiseType {
12
19
  then: () => unknown;
13
20
  }
21
+ /** Any non-promise typed value, excluding thenables. */
14
22
  export type AnyNonPromise = Exclude<TypedValue, PromiseType>;
15
23
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE/C,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AACvE,MAAM,MAAM,eAAe,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AAC9D,MAAM,MAAM,kBAAkB,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,UAAU,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,CAAC,CAAA;AAC3E,MAAM,MAAM,uBAAuB,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,eAAe,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,CAAC,CAAA;AACrF,MAAM,MAAM,kBAAkB,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,UAAU,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,CAAA;AACtE,MAAM,MAAM,uBAAuB,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,IAAI,eAAe,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,CAAA;AAEhF,4EAA4E;AAC5E,MAAM,MAAM,UAAU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4EAA4E;AAC5E,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAA;AAEtC,oEAAoE;AACpE,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,OAAO,CAAA;CACpB;AAED,wDAAwD;AACxD,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,UAAU,EAAE,WAAW,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/promise",
3
- "version": "5.0.83",
3
+ "version": "5.0.84",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "promise",
@@ -42,12 +42,12 @@
42
42
  "!**/*.test.*"
43
43
  ],
44
44
  "dependencies": {
45
- "@xylabs/typeof": "~5.0.83"
45
+ "@xylabs/typeof": "~5.0.84"
46
46
  },
47
47
  "devDependencies": {
48
- "@xylabs/ts-scripts-yarn3": "~7.4.11",
49
- "@xylabs/tsconfig": "~7.4.11",
50
- "@xylabs/vitest-extended": "~5.0.83",
48
+ "@xylabs/ts-scripts-yarn3": "~7.4.13",
49
+ "@xylabs/tsconfig": "~7.4.13",
50
+ "@xylabs/vitest-extended": "~5.0.84",
51
51
  "typescript": "~5.9.3",
52
52
  "vitest": "~4.0.18"
53
53
  },