@byloth/core 2.0.0-rc.9 → 2.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.
Files changed (47) hide show
  1. package/dist/core.js +3371 -608
  2. package/dist/core.js.map +1 -1
  3. package/dist/core.umd.cjs +2 -2
  4. package/dist/core.umd.cjs.map +1 -1
  5. package/package.json +13 -10
  6. package/src/core/types.ts +41 -0
  7. package/src/helpers.ts +11 -2
  8. package/src/index.ts +12 -9
  9. package/src/models/aggregators/aggregated-async-iterator.ts +765 -21
  10. package/src/models/aggregators/aggregated-iterator.ts +698 -22
  11. package/src/models/aggregators/reduced-iterator.ts +699 -10
  12. package/src/models/aggregators/types.ts +153 -10
  13. package/src/models/callbacks/callable-object.ts +42 -6
  14. package/src/models/callbacks/index.ts +2 -2
  15. package/src/models/callbacks/publisher.ts +139 -4
  16. package/src/models/callbacks/switchable-callback.ts +138 -4
  17. package/src/models/callbacks/types.ts +16 -0
  18. package/src/models/exceptions/core.ts +112 -3
  19. package/src/models/exceptions/index.ts +340 -13
  20. package/src/models/index.ts +4 -8
  21. package/src/models/iterators/smart-async-iterator.ts +687 -22
  22. package/src/models/iterators/smart-iterator.ts +631 -21
  23. package/src/models/iterators/types.ts +268 -9
  24. package/src/models/json/json-storage.ts +388 -110
  25. package/src/models/json/types.ts +10 -1
  26. package/src/models/promises/deferred-promise.ts +75 -5
  27. package/src/models/promises/index.ts +1 -3
  28. package/src/models/promises/smart-promise.ts +232 -4
  29. package/src/models/promises/timed-promise.ts +38 -1
  30. package/src/models/promises/types.ts +84 -2
  31. package/src/models/timers/clock.ts +91 -19
  32. package/src/models/timers/countdown.ts +152 -22
  33. package/src/models/timers/game-loop.ts +243 -0
  34. package/src/models/timers/index.ts +2 -1
  35. package/src/models/types.ts +6 -5
  36. package/src/utils/async.ts +43 -0
  37. package/src/utils/curve.ts +75 -0
  38. package/src/utils/date.ts +204 -10
  39. package/src/utils/dom.ts +16 -2
  40. package/src/utils/index.ts +3 -2
  41. package/src/utils/iterator.ts +200 -17
  42. package/src/utils/math.ts +55 -3
  43. package/src/utils/random.ts +109 -2
  44. package/src/utils/string.ts +11 -0
  45. package/src/models/game-loop.ts +0 -83
  46. package/src/models/promises/long-running-task.ts +0 -294
  47. package/src/models/promises/thenable.ts +0 -97
@@ -2,11 +2,69 @@ import type { PromiseResolver, PromiseRejecter, FulfilledHandler, RejectedHandle
2
2
 
3
3
  import SmartPromise from "./smart-promise.js";
4
4
 
5
+ /**
6
+ * A class representing a {@link SmartPromise} that can be resolved or rejected from the "outside".
7
+ * The `resolve` and `reject` methods are exposed to allow the promise to be settled from another context.
8
+ *
9
+ * It's particularly useful in scenarios where the promise is created and needs to be awaited in one place,
10
+ * while being resolved or rejected in another (e.g. an event handler for an user interaction).
11
+ *
12
+ * This is a change in the approach to promises: instead of defining how the promise will be resolved (or rejected),
13
+ * you define how to handle the resolution (or rejection) when it occurs.
14
+ *
15
+ * ```ts
16
+ * const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
17
+ *
18
+ * promise.then((result) => console.log(result)); // ["Hello,", "World!"]
19
+ * promise.resolve("Hello, World!");
20
+ * ```
21
+ *
22
+ * @template T The type of value the promise expects to initially be resolved with. Default is `void`.
23
+ * @template F
24
+ * The type of value returned by the `onFulfilled` callback.
25
+ * This will be the actual type of value the promise will eventually resolve to. Default is `T`.
26
+ * @template R
27
+ * The type of value possibly returned by the `onRejected` callback.
28
+ * This will be coupled with the type of value the promise will eventually resolve to, if provided. Default is `never`.
29
+ */
5
30
  export default class DeferredPromise<T = void, F = T, R = never> extends SmartPromise<F | R>
6
31
  {
32
+ /**
33
+ * The exposed function that allows to resolve the promise.
34
+ *
35
+ * This protected property is the only one that can be modified directly by the derived classes.
36
+ * If you're looking for the public and readonly property, use the {@link DeferredPromise.resolve} getter instead.
37
+ */
7
38
  protected _resolve: PromiseResolver<T>;
39
+
40
+ /**
41
+ * The exposed function that allows to reject the promise.
42
+ */
43
+ public get resolve(): PromiseResolver<T> { return this._resolve; }
44
+
45
+ /**
46
+ * The exposed function that allows to reject the promise.
47
+ *
48
+ * This protected property is the only one that can be modified directly by the derived classes.
49
+ * If you're looking for the public and readonly property, use the {@link DeferredPromise.reject} getter instead.
50
+ */
8
51
  protected _reject: PromiseRejecter;
9
52
 
53
+ /**
54
+ * The exposed function that allows to reject the promise.
55
+ */
56
+ public get reject(): PromiseRejecter { return this._reject; }
57
+
58
+ /**
59
+ * Initializes a new instance of the {@link DeferredPromise} class.
60
+ *
61
+ * ```ts
62
+ * const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
63
+ * ```
64
+ *
65
+ * @param onFulfilled The callback to execute once the promise is fulfilled.
66
+ * @param onRejected The callback to execute once the promise is rejected.
67
+ */
10
68
  public constructor(onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<unknown, R> | null)
11
69
  {
12
70
  let _resolve: PromiseResolver<T>;
@@ -21,15 +79,27 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
21
79
  _reject = reject;
22
80
  });
23
81
 
24
- this._promise.then(onFulfilled as FulfilledHandler<F | R>, onRejected);
82
+ this._promise = this._promise.then(onFulfilled as FulfilledHandler<F | R>, onRejected);
25
83
 
26
84
  this._resolve = _resolve!;
27
85
  this._reject = _reject!;
28
86
  }
29
87
 
30
- public get resolve(): PromiseResolver<T> { return this._resolve; }
31
- public get reject(): PromiseRejecter { return this._reject; }
32
-
88
+ /**
89
+ * Watches another promise and resolves or rejects this promise when the other one is settled.
90
+ *
91
+ * ```ts
92
+ * const promise = new Promise<string>((resolve) => setTimeout(() => resolve("Hello, World!"), 1_000));
93
+ * const deferred = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
94
+ *
95
+ * deferred.then((result) => console.log(result)); // ["Hello,", "World!"]
96
+ * deferred.watch(promise);
97
+ * ```
98
+ *
99
+ * @param otherPromise The promise to watch.
100
+ *
101
+ * @returns The current instance of the {@link DeferredPromise} class.
102
+ */
33
103
  public watch(otherPromise: PromiseLike<T>): this
34
104
  {
35
105
  otherPromise.then(this.resolve, this.reject);
@@ -37,5 +107,5 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
37
107
  return this;
38
108
  }
39
109
 
40
- public readonly [Symbol.toStringTag]: string = "DeferredPromise";
110
+ public override readonly [Symbol.toStringTag]: string = "DeferredPromise";
41
111
  }
@@ -1,7 +1,5 @@
1
1
  import DeferredPromise from "./deferred-promise.js";
2
- import LongRunningTask from "./long-running-task.js";
3
2
  import SmartPromise from "./smart-promise.js";
4
- import Thenable from "./thenable.js";
5
3
  import TimedPromise from "./timed-promise.js";
6
4
 
7
- export { DeferredPromise, LongRunningTask, SmartPromise, Thenable, TimedPromise };
5
+ export { DeferredPromise, SmartPromise, TimedPromise };
@@ -1,18 +1,121 @@
1
1
  import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types.js";
2
2
 
3
+ /**
4
+ * A wrapper class representing an enhanced version of the native {@link Promise} object.
5
+ *
6
+ * It provides additional properties to check the state of the promise itself.
7
+ * The state can be either `pending`, `fulfilled` or `rejected` and is accessible through
8
+ * the {@link SmartPromise.isPending}, {@link SmartPromise.isFulfilled} & {@link SmartPromise.isRejected} properties.
9
+ *
10
+ * ```ts
11
+ * const promise = new SmartPromise<string>((resolve, reject) =>
12
+ * {
13
+ * setTimeout(() => resolve("Hello, World!"), 1_000);
14
+ * });
15
+ *
16
+ * console.log(promise.isPending); // true
17
+ * console.log(promise.isFulfilled); // false
18
+ *
19
+ * console.log(await promise); // "Hello, World!"
20
+ *
21
+ * console.log(promise.isPending); // false
22
+ * console.log(promise.isFulfilled); // true
23
+ * ```
24
+ *
25
+ * @template T The type of value the promise will eventually resolve to. Default is `void`.
26
+ */
3
27
  export default class SmartPromise<T = void> implements Promise<T>
4
28
  {
29
+ /**
30
+ * Wraps a new {@link SmartPromise} object around an existing native {@link Promise} object.
31
+ *
32
+ * ```ts
33
+ * const request = fetch("https://api.example.com/data");
34
+ * const smartRequest = SmartPromise.FromPromise(request);
35
+ *
36
+ * console.log(request.isPending); // Throws an error: `isPending` is not a property of `Promise`.
37
+ * console.log(smartRequest.isPending); // true
38
+ *
39
+ * const response = await request;
40
+ * console.log(smartRequest.isFulfilled); // true
41
+ * ```
42
+ *
43
+ * @param promise The promise to wrap.
44
+ *
45
+ * @returns A new {@link SmartPromise} object that wraps the provided promise.
46
+ */
5
47
  public static FromPromise<T>(promise: Promise<T>): SmartPromise<T>
6
48
  {
7
49
  return new SmartPromise((resolve, reject) => promise.then(resolve, reject));
8
50
  }
9
51
 
52
+ /**
53
+ * A flag indicating whether the promise is still pending or not.
54
+ *
55
+ * The protected property is the only one that can be modified directly by the derived classes.
56
+ * If you're looking for the public and readonly property, use the {@link SmartPromise.isPending} getter instead.
57
+ */
10
58
  protected _isPending: boolean;
59
+
60
+ /**
61
+ * A flag indicating whether the promise is still pending or not.
62
+ */
63
+ public get isPending(): boolean
64
+ {
65
+ return this._isPending;
66
+ }
67
+
68
+ /**
69
+ * A flag indicating whether the promise has been fulfilled or not.
70
+ *
71
+ * The protected property is the only one that can be modified directly by the derived classes.
72
+ * If you're looking for the public and readonly property, use the {@link SmartPromise.isFulfilled} getter instead.
73
+ */
11
74
  protected _isFulfilled: boolean;
75
+
76
+ /**
77
+ * A flag indicating whether the promise has been fulfilled or not.
78
+ */
79
+ public get isFulfilled(): boolean
80
+ {
81
+ return this._isFulfilled;
82
+ }
83
+
84
+ /**
85
+ * A flag indicating whether the promise has been rejected or not.
86
+ *
87
+ * The protected property is the only one that can be modified directly by the derived classes.
88
+ * If you're looking for the public and readonly property, use the {@link SmartPromise.isRejected} getter instead.
89
+ */
12
90
  protected _isRejected: boolean;
13
91
 
92
+ /**
93
+ * A flag indicating whether the promise has been rejected or not.
94
+ */
95
+ public get isRejected(): boolean
96
+ {
97
+ return this._isRejected;
98
+ }
99
+
100
+ /**
101
+ * The native {@link Promise} object wrapped by this instance.
102
+ */
14
103
  protected _promise: Promise<T>;
15
104
 
105
+ /**
106
+ * Initializes a new instance of the {@link SmartPromise} class.
107
+ *
108
+ * ```ts
109
+ * const promise = new SmartPromise<string>((resolve, reject) =>
110
+ * {
111
+ * setTimeout(() => resolve("Hello, World!"), 1_000);
112
+ * });
113
+ * ```
114
+ *
115
+ * @param executor
116
+ * The function responsible for eventually resolving or rejecting the promise.
117
+ * Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
118
+ */
16
119
  public constructor(executor: PromiseExecutor<T>)
17
120
  {
18
121
  this._isPending = true;
@@ -38,12 +141,76 @@ export default class SmartPromise<T = void> implements Promise<T>
38
141
  .then(_onFulfilled, _onRejected);
39
142
  }
40
143
 
41
- public get isPending(): boolean { return this._isPending; }
42
- public get isFulfilled(): boolean { return this._isFulfilled; }
43
- public get isRejected(): boolean { return this._isRejected; }
44
-
144
+ /**
145
+ * Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
146
+ *
147
+ * ```ts
148
+ * const promise = new SmartPromise<string>((resolve, reject) =>
149
+ * {
150
+ * setTimeout(() => resolve("Hello, World!"), 1_000);
151
+ * });
152
+ *
153
+ * console.log(await promise.then()); // "Hello, World!"
154
+ * ```
155
+ *
156
+ * @returns A new {@link Promise} identical to the original one.
157
+ */
45
158
  public then(onFulfilled?: null): Promise<T>;
159
+
160
+ /**
161
+ * Attaches a callback that executes right after the promise is fulfilled.
162
+ *
163
+ * The previous result of the promise is passed as the argument to the callback.
164
+ * The callback's return value is considered the new promise's result instead.
165
+ *
166
+ * ```ts
167
+ * const promise = new SmartPromise<string>((resolve, reject) =>
168
+ * {
169
+ * setTimeout(() => resolve("Hello, World!"), 1_000);
170
+ * });
171
+ *
172
+ * promise.then((result) => console.log(result)); // "Hello, World!"
173
+ * ```
174
+ *
175
+ * @template F The type of value the new promise will eventually resolve to. Default is `T`.
176
+ *
177
+ * @param onFulfilled The callback to execute once the promise is fulfilled.
178
+ *
179
+ * @returns A new {@link Promise} resolved with the return value of the callback.
180
+ */
46
181
  public then<F = T>(onFulfilled: FulfilledHandler<T, F>, onRejected?: null): Promise<F>;
182
+
183
+ /**
184
+ * Attaches callbacks that executes right after the promise is fulfilled or rejected.
185
+ *
186
+ * The previous result of the promise is passed as the argument to the fulfillment callback.
187
+ * The fulfillment callback's return value is considered the new promise's result instead.
188
+ *
189
+ * If an error is thrown during execution, the rejection callback is then executed instead.
190
+ *
191
+ * Also note that:
192
+ * - If the rejection callback runs properly, the error is considered handled.
193
+ * The rejection callback's return value is considered the new promise's result.
194
+ * - If the rejection callback throws an error, the new promise is rejected with that error.
195
+ *
196
+ * ```ts
197
+ * const promise = new SmartPromise((resolve, reject) =>
198
+ * {
199
+ * setTimeout(resolve, Math.random() * 1_000);
200
+ * setTimeout(reject, Math.random() * 1_000);
201
+ * });
202
+ *
203
+ * promise.then(() => console.log("OK!"), () => console.log("KO!")); // "OK!" or "KO!"
204
+ * ```
205
+ *
206
+ * @template F The type of value the new promise will eventually resolve to. Default is `T`.
207
+ * @template R The type of value the new promise will eventually resolve to. Default is `never`.
208
+ *
209
+ * @param onFulfilled The callback to execute once the promise is fulfilled.
210
+ * @param onRejected The callback to execute once the promise is rejected.
211
+ *
212
+ * @returns A new {@link Promise} resolved or rejected based on the callbacks.
213
+ */
47
214
  public then<F = T, R = never>(onFulfilled: FulfilledHandler<T, F>, onRejected: RejectedHandler<unknown, R>)
48
215
  : Promise<F | R>;
49
216
  public then<F = T, R = never>(
@@ -53,12 +220,73 @@ export default class SmartPromise<T = void> implements Promise<T>
53
220
  return this._promise.then(onFulfilled, onRejected);
54
221
  }
55
222
 
223
+ /**
224
+ * Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
225
+ *
226
+ * ```ts
227
+ * const promise = new SmartPromise((resolve, reject) =>
228
+ * {
229
+ * setTimeout(() => reject(new Error("An unknown error occurred.")), 1_000);
230
+ * });
231
+ *
232
+ * promise.catch(); // Uncaught Error: An unknown error occurred.
233
+ * ```
234
+ *
235
+ * @returns A new {@link Promise} identical to the original one.
236
+ */
56
237
  public catch(onRejected?: null): Promise<T>;
238
+
239
+ /**
240
+ * Attaches a callback to handle the potential rejection of the promise.
241
+ * If it happens, the callback is then executed.
242
+ *
243
+ * Also note that:
244
+ * - If the callback runs properly, the error is considered handled.
245
+ * The callback's return value is considered the new promise's result.
246
+ * - If the callback throws an error, the new promise is rejected with that error.
247
+ *
248
+ * ```ts
249
+ * const promise = new SmartPromise((resolve, reject) =>
250
+ * {
251
+ * setTimeout(() => reject(new Error("An unknown error occurred.")), 1_000);
252
+ * });
253
+ *
254
+ * promise.catch((reason) => console.error(reason)); // "Error: An unknown error occurred."
255
+ * ```
256
+ *
257
+ * @template R The type of value the new promise will eventually resolve to. Default is `T`.
258
+ *
259
+ * @param onRejected The callback to execute once the promise is rejected.
260
+ *
261
+ * @returns A new {@link Promise} able to catch and handle the potential error.
262
+ */
57
263
  public catch<R = never>(onRejected: RejectedHandler<unknown, R>): Promise<T | R>;
58
264
  public catch<R = never>(onRejected?: RejectedHandler<unknown, R> | null): Promise<T | R>
59
265
  {
60
266
  return this._promise.catch(onRejected);
61
267
  }
268
+
269
+ /**
270
+ * Attaches a callback that executes right after the promise is settled, regardless of the outcome.
271
+ *
272
+ * ```ts
273
+ * const promise = new SmartPromise((resolve, reject) =>
274
+ * {
275
+ * setTimeout(resolve, Math.random() * 1_000);
276
+ * setTimeout(reject, Math.random() * 1_000);
277
+ * });
278
+ *
279
+ *
280
+ * promise
281
+ * .then(() => console.log("OK!")) // Logs "OK!" if the promise is fulfilled.
282
+ * .catch(() => console.log("KO!")) // Logs "KO!" if the promise is rejected.
283
+ * .finally(() => console.log("Done!")); // Always logs "Done!".
284
+ * ```
285
+ *
286
+ * @param onFinally The callback to execute when once promise is settled.
287
+ *
288
+ * @returns A new {@link Promise} that executes the callback once the promise is settled.
289
+ */
62
290
  public finally(onFinally?: (() => void) | null): Promise<T>
63
291
  {
64
292
  return this._promise.finally(onFinally);
@@ -3,8 +3,45 @@ import { TimeoutException } from "../exceptions/index.js";
3
3
  import SmartPromise from "./smart-promise.js";
4
4
  import type { MaybePromise, PromiseExecutor } from "./types.js";
5
5
 
6
+ /**
7
+ * A class representing a {@link SmartPromise} that rejects automatically after a given time.
8
+ * It's useful for operations that must be completed within a certain time frame.
9
+ *
10
+ * If the operation takes longer than the specified time, the promise is rejected with a {@link TimeoutException}.
11
+ *
12
+ * ```ts
13
+ * const promise = new TimedPromise<string>((resolve, reject) =>
14
+ * {
15
+ * setTimeout(() => resolve("Hello, World!"), Math.random() * 10_000);
16
+ *
17
+ * }, 5_000);
18
+ *
19
+ * promise
20
+ * .then((result) => console.log(result)) // "Hello, World!"
21
+ * .catch((error) => console.error(error)); // TimeoutException: The operation has timed out.
22
+ * ```
23
+ *
24
+ * @template T The type of value the promise will eventually resolve to. Default is `void`.
25
+ */
6
26
  export default class TimedPromise<T = void> extends SmartPromise<T>
7
27
  {
28
+ /**
29
+ * Initializes a new instance of the {@link TimedPromise} class.
30
+ *
31
+ * ```ts
32
+ * const promise = new TimedPromise<string>((resolve, reject) =>
33
+ * {
34
+ * setTimeout(() => resolve("Hello, World!"), Math.random() * 10_000);
35
+ *
36
+ * }, 5_000);
37
+ * ```
38
+ *
39
+ * @param executor
40
+ * The function responsible for eventually resolving or rejecting the promise.
41
+ * Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
42
+ *
43
+ * @param timeout The maximum time in milliseconds that the operation can take before timing out.
44
+ */
8
45
  public constructor(executor: PromiseExecutor<T>, timeout?: number)
9
46
  {
10
47
  super((resolve, reject) =>
@@ -27,5 +64,5 @@ export default class TimedPromise<T = void> extends SmartPromise<T>
27
64
  });
28
65
  }
29
66
 
30
- public readonly [Symbol.toStringTag]: string = "TimedPromise";
67
+ public override readonly [Symbol.toStringTag]: string = "TimedPromise";
31
68
  }
@@ -1,10 +1,92 @@
1
- export type { LongRunningTaskOptions } from "./long-running-task.js";
2
-
1
+ /**
2
+ * An union type that represents a value that can be either a value or a promise of that value.
3
+ * This is useful when you want to handle both synchronous and asynchronous values in the same way.
4
+ *
5
+ * ```ts
6
+ * async function splitWords(value: MaybePromise<string>): Promise<string[]>
7
+ * {
8
+ * return (await value).split(" ");
9
+ * }
10
+ * ```
11
+ *
12
+ * @template T The type of the value.
13
+ */
3
14
  export type MaybePromise<T> = T | PromiseLike<T>;
4
15
 
16
+ /**
17
+ * An utility type that represents the callback that is executed when a promise is fulfilled.
18
+ * It's compatible with the `onFulfilled` parameter of the `then` method of the native {@link Promise} object.
19
+ *
20
+ * ```ts
21
+ * const onFulfilled: FulfilledHandler<string, string[]> = (value) => value.split(" ");
22
+ *
23
+ * await new Promise<string>((resolve) => resolve("Hello, World!"))
24
+ * .then(onFulfilled);
25
+ * ```
26
+ *
27
+ * @template T The type of value accepted by the function. Default is `void`.
28
+ * @template R The type of value returned by the function. Default is `T`.
29
+ */
5
30
  export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
31
+
32
+ /**
33
+ * An utility type that represents the callback that is executed when a promise is rejected.
34
+ * It's compatible with the `onRejected` parameter of the `then`/`catch` methods of the native {@link Promise} object.
35
+ *
36
+ * ```ts
37
+ * const onRejected: RejectedHandler<unknown, string> = (reason) => String(reason);
38
+ *
39
+ * await new Promise<string>((_, reject) => reject(new Error("An error occurred.")))
40
+ * .catch(onRejected);
41
+ * ```
42
+ *
43
+ * @template E The type of value accepted by the function. Default is `unknown`.
44
+ * @template R The type of value returned by the function. Default is `never`.
45
+ */
6
46
  export type RejectedHandler<E = unknown, R = never> = (reason: E) => MaybePromise<R>;
7
47
 
48
+ /**
49
+ * An utility type that represents a function that can be used to resolve a promise.
50
+ * It's compatible with the `resolve` parameter of the native {@link Promise} executor.
51
+ *
52
+ * ```ts
53
+ * let _resolve: PromiseResolver<string> = (result) => console.log(result);
54
+ *
55
+ * await new Promise<string>((resolve) => { _resolve = resolve; });
56
+ * ```
57
+ *
58
+ * @template T The type of the value accepted by the function. Default is `void`.
59
+ */
8
60
  export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
61
+
62
+ /**
63
+ * An utility type that represents a function that can be used to reject a promise.
64
+ * It's compatible with the `reject` parameter of the native {@link Promise} executor.
65
+ *
66
+ * ```ts
67
+ * let _reject: PromiseRejecter<string> = (reason) => console.error(reason);
68
+ *
69
+ * await new Promise<string>((_, reject) => { _reject = reject; });
70
+ * ```
71
+ *
72
+ * @template E The type of the value accepted by the function. Default is `unknown`.
73
+ */
9
74
  export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
75
+
76
+ /**
77
+ * An utility type that represents the function that will be executed by the promise.
78
+ * It's compatible with the `executor` parameter of the native {@link Promise} object.
79
+ *
80
+ * ```ts
81
+ * const executor: PromiseExecutor<string> = (resolve, reject) =>
82
+ * {
83
+ * setTimeout(() => resolve("Hello, World!"), 1_000);
84
+ * };
85
+ *
86
+ * await new Promise<string>(executor);
87
+ * ```
88
+ *
89
+ * @template T The type of value accepted by the `resolve` function. Default is `void`.
90
+ * @template E The type of value accepted by the `reject` function. Default is `unknown`.
91
+ */
10
92
  export type PromiseExecutor<T = void, E = unknown> = (resolve: PromiseResolver<T>, reject: PromiseRejecter<E>) => void;