@handy-common-utils/promise-utils 1.6.0 → 1.7.1
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 +1317 -466
- package/dist/promise-utils.d.ts +289 -22
- package/dist/promise-utils.d.ts.map +1 -1
- package/dist/promise-utils.js +386 -33
- package/dist/promise-utils.js.map +1 -0
- package/package.json +2 -2
package/dist/promise-utils.d.ts
CHANGED
|
@@ -25,11 +25,12 @@ export declare const EXPONENTIAL_SEQUENCE: number[];
|
|
|
25
25
|
/**
|
|
26
26
|
* The state of a Promise can only be one of: Pending, Fulfilled, and Rejected.
|
|
27
27
|
*/
|
|
28
|
-
export declare
|
|
29
|
-
Pending
|
|
30
|
-
Fulfilled
|
|
31
|
-
Rejected
|
|
32
|
-
}
|
|
28
|
+
export declare const PromiseState: {
|
|
29
|
+
readonly Pending: "Pending";
|
|
30
|
+
readonly Fulfilled: "Fulfilled";
|
|
31
|
+
readonly Rejected: "Rejected";
|
|
32
|
+
};
|
|
33
|
+
export type PromiseStateType = keyof typeof PromiseState;
|
|
33
34
|
export declare abstract class PromiseUtils {
|
|
34
35
|
/**
|
|
35
36
|
* Executes an operation repeatedly and collects all the results.
|
|
@@ -149,21 +150,76 @@ export declare abstract class PromiseUtils {
|
|
|
149
150
|
static inParallel<Data, Result, TError = Result>(parallelism: number, jobs: Iterable<Data>, operation: (job: Data, index: number) => Promise<Result>, options?: {
|
|
150
151
|
abortOnError: boolean;
|
|
151
152
|
}): Promise<Array<Result | TError>>;
|
|
153
|
+
/**
|
|
154
|
+
* Creates a cancellable timer that will resolve after a specified number of milliseconds.
|
|
155
|
+
*
|
|
156
|
+
* The returned object contains:
|
|
157
|
+
* - `stop()` to cancel the scheduled resolution (if called before the timer fires). Calling
|
|
158
|
+
* `stop()` prevents the promise from being settled by this timer.
|
|
159
|
+
* - `promise` which will resolve with the supplied `result` (or the value returned by the
|
|
160
|
+
* `result` function) after `ms` milliseconds unless `stop()` is called first.
|
|
161
|
+
*
|
|
162
|
+
* Note: If the `result` is a function that returns a Promise, the returned `promise` will
|
|
163
|
+
* resolve with that Promise's resolution (i.e. it behaves like resolving with a PromiseLike).
|
|
164
|
+
*
|
|
165
|
+
* @param ms The number of milliseconds after which the scheduled resolution will occur.
|
|
166
|
+
* @param result The result to be resolved by the Promise, or a function that supplies the result.
|
|
167
|
+
* @returns An object with `stop()` and `promise`.
|
|
168
|
+
*/
|
|
169
|
+
static cancellableDelayedResolve<T>(ms: number, result?: T | PromiseLike<T> | (() => (T | PromiseLike<T>))): {
|
|
170
|
+
stop: () => void;
|
|
171
|
+
promise: Promise<T>;
|
|
172
|
+
};
|
|
152
173
|
/**
|
|
153
174
|
* Creates a Promise that resolves after a specified number of milliseconds.
|
|
154
175
|
*
|
|
176
|
+
* The `result` argument may be:
|
|
177
|
+
* - a value to resolve with,
|
|
178
|
+
* - a PromiseLike whose resolution will be adopted by the returned Promise, or
|
|
179
|
+
* - a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
180
|
+
*
|
|
181
|
+
* If `result` is a function, it is called when the timer elapses; if it returns a Promise,
|
|
182
|
+
* the returned Promise will adopt that Promise's outcome.
|
|
183
|
+
*
|
|
155
184
|
* @param ms The number of milliseconds after which the created Promise will resolve.
|
|
156
185
|
* @param result The result to be resolved by the Promise, or a function that supplies the result.
|
|
157
|
-
* @returns A
|
|
186
|
+
* @returns A Promise that resolves with the specified result after the specified delay.
|
|
158
187
|
*/
|
|
159
188
|
static delayedResolve<T>(ms: number, result?: T | PromiseLike<T> | (() => (T | PromiseLike<T>))): Promise<T>;
|
|
189
|
+
/**
|
|
190
|
+
* Creates a cancellable timer that will reject after a specified number of milliseconds.
|
|
191
|
+
*
|
|
192
|
+
* The returned object contains:
|
|
193
|
+
* - `stop()` to cancel the scheduled rejection (if called before the timer fires). Calling
|
|
194
|
+
* `stop()` prevents the promise from being settled by this timer.
|
|
195
|
+
* - `promise` which will reject with the supplied `reason` (or the value returned by the
|
|
196
|
+
* `reason` function) after `ms` milliseconds unless `stop()` is called first.
|
|
197
|
+
*
|
|
198
|
+
* If the `reason` is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
|
|
199
|
+
*
|
|
200
|
+
* @param ms The number of milliseconds after which the scheduled rejection will occur.
|
|
201
|
+
* @param reason The reason for the rejection, or a function that supplies the reason.
|
|
202
|
+
* @returns An object with `stop()` and `promise`.
|
|
203
|
+
*/
|
|
204
|
+
static cancellableDelayedReject<T = never, R = any>(ms: number, reason: R | PromiseLike<R> | (() => R | PromiseLike<R>)): {
|
|
205
|
+
stop: () => void;
|
|
206
|
+
promise: Promise<T>;
|
|
207
|
+
};
|
|
160
208
|
/**
|
|
161
209
|
* Creates a Promise that rejects after a specified number of milliseconds.
|
|
162
210
|
*
|
|
211
|
+
* The `reason` argument may be:
|
|
212
|
+
* - a value to reject with,
|
|
213
|
+
* - a PromiseLike whose rejection will be adopted by the returned Promise, or
|
|
214
|
+
* - a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
215
|
+
*
|
|
216
|
+
* If `reason` is a function, it is called when the timer elapses; if it returns a Promise,
|
|
217
|
+
* the returned Promise will reject with that Promise's rejection reason (or reject with the
|
|
218
|
+
* returned value if it resolves).
|
|
219
|
+
*
|
|
163
220
|
* @param ms The number of milliseconds after which the created Promise will reject.
|
|
164
221
|
* @param reason The reason for the rejection, or a function that supplies the reason.
|
|
165
|
-
*
|
|
166
|
-
* @returns A new Promise that rejects with the specified reason after the specified delay.
|
|
222
|
+
* @returns A Promise that rejects with the specified reason after the specified delay.
|
|
167
223
|
*/
|
|
168
224
|
static delayedReject<T = never, R = any>(ms: number, reason: R | PromiseLike<R> | (() => R | PromiseLike<R>)): Promise<T>;
|
|
169
225
|
/**
|
|
@@ -200,7 +256,7 @@ export declare abstract class PromiseUtils {
|
|
|
200
256
|
* @param p The Promise whose state is to be determined.
|
|
201
257
|
* @returns A Promise that resolves immediately with the state of the input Promise.
|
|
202
258
|
*/
|
|
203
|
-
static promiseState(p: Promise<any>): Promise<
|
|
259
|
+
static promiseState(p: Promise<any>): Promise<PromiseStateType>;
|
|
204
260
|
private static synchronizationLocks;
|
|
205
261
|
/**
|
|
206
262
|
* Provides mutual exclusion similar to `synchronized` in Java.
|
|
@@ -214,57 +270,268 @@ export declare abstract class PromiseUtils {
|
|
|
214
270
|
* @param operation The function that performs the computation and returns a Promise.
|
|
215
271
|
* @returns The result of the operation function.
|
|
216
272
|
*/
|
|
217
|
-
static synchronized<T>(lock: any, operation: (previousState:
|
|
273
|
+
static synchronized<T>(lock: any, operation: (previousState: PromiseStateType | undefined, previousSettledState: PromiseStateType | undefined, previousResult: any) => Promise<T>): Promise<T>;
|
|
218
274
|
/**
|
|
219
275
|
* This is just another spelling of {@link PromiseUtils.synchronized}.
|
|
220
276
|
* @param lock The object (such as a string, a number, or `this` in a class) used to identify the lock.
|
|
221
277
|
* @param operation The function that performs the computation and returns a Promise.
|
|
222
278
|
* @returns The result of the operation function.
|
|
223
279
|
*/
|
|
224
|
-
static synchronised<T>(lock: any, operation: (previousState:
|
|
280
|
+
static synchronised<T>(lock: any, operation: (previousState: PromiseStateType | undefined, previousSettledState: PromiseStateType | undefined, previousResult: any) => Promise<T>): Promise<T>;
|
|
281
|
+
/**
|
|
282
|
+
* Runs an operation periodically with configurable intervals and stopping conditions.
|
|
283
|
+
*
|
|
284
|
+
* - `interval` may be a single number (ms), an array of numbers, or a function
|
|
285
|
+
* that receives the iteration number (starting at 1) and returns the next
|
|
286
|
+
* interval in milliseconds or `undefined` to stop.
|
|
287
|
+
* - If the interval array runs out of elements or the function returns `undefined`
|
|
288
|
+
* (or a negative value), no further invocations will be scheduled.
|
|
289
|
+
*
|
|
290
|
+
* Options:
|
|
291
|
+
* - `maxExecutions` stop after N runs (inclusive).
|
|
292
|
+
* - `maxDurationMs` stop after elapsed ms since the first scheduled start.
|
|
293
|
+
* - `schedule` controls how the interval is measured:
|
|
294
|
+
* - `'delayAfterEnd'`: wait the interval after the previous operation completes
|
|
295
|
+
* before scheduling the next one (equivalent to a fixed delay between ends).
|
|
296
|
+
* - `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured
|
|
297
|
+
* between the starts of successive operations).
|
|
298
|
+
* The default schedule is `'delayBetweenStarts'`.
|
|
299
|
+
*
|
|
300
|
+
* Returns an object with `stop()` to cancel further executions and `done` which
|
|
301
|
+
* resolves when the periodic runner stops. If the provided `operation` throws or
|
|
302
|
+
* rejects, the `done` promise will reject with that error so callers can handle it.
|
|
303
|
+
*
|
|
304
|
+
* Note: The first invocation of `operation` is scheduled after the first interval
|
|
305
|
+
* elapses (i.e. this function does NOT call `operation` immediately). If you need
|
|
306
|
+
* an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`.
|
|
307
|
+
*
|
|
308
|
+
* @template T The operation return type (ignored by the runner; used for typing).
|
|
309
|
+
* @param operation Function to run each iteration. Receives the iteration index (1-based).
|
|
310
|
+
* @param interval Number | number[] | ((iteration: number) => number|undefined) defining waits.
|
|
311
|
+
* @param options Optional configuration.
|
|
312
|
+
* @param options.maxExecutions Stop after N executions.
|
|
313
|
+
* @param options.maxDurationMs Stop after N milliseconds.
|
|
314
|
+
* @param options.schedule How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`.
|
|
315
|
+
* @returns An object containing `stop()` to cancel further executions and `done` Promise
|
|
316
|
+
* which resolves when the periodic runner stops (or rejects if the operation errors).
|
|
317
|
+
*/
|
|
318
|
+
static runPeriodically<T>(operation: (iteration: number) => Promise<T> | T, interval: number | Array<number> | ((iteration: number) => number | undefined), options?: {
|
|
319
|
+
maxExecutions?: number;
|
|
320
|
+
maxDurationMs?: number;
|
|
321
|
+
schedule?: 'delayAfterEnd' | 'delayBetweenStarts';
|
|
322
|
+
}): {
|
|
323
|
+
stop: () => void;
|
|
324
|
+
done: Promise<void>;
|
|
325
|
+
};
|
|
225
326
|
}
|
|
226
327
|
/**
|
|
227
|
-
*
|
|
328
|
+
* Executes an operation repeatedly and collects all the results.
|
|
329
|
+
* This function is very useful for many scenarios, such like client-side pagination.
|
|
330
|
+
*
|
|
331
|
+
* @param operation A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging.
|
|
332
|
+
* @param nextParameter A function for calculating the next parameter from the operation result. Normally, this parameter controls paging. This function should return null when no further invocation of the operation function is desired. If further invocation is desired, the return value of this function can be a Promise or a non-Promise value.
|
|
333
|
+
* @param collect A function for merging the operation result into the collection.
|
|
334
|
+
* @param initialCollection The initial collection, which will be the first argument passed to the first invocation of the collect function.
|
|
335
|
+
* @param initialParameter The parameter for the first operation.
|
|
336
|
+
* @returns A promise that resolves to a collection of all the results returned by the operation function.
|
|
228
337
|
*/
|
|
229
338
|
export declare const repeat: typeof PromiseUtils.repeat;
|
|
230
339
|
/**
|
|
231
|
-
*
|
|
340
|
+
* Repeatedly performs an operation until a specified criteria is met.
|
|
341
|
+
*
|
|
342
|
+
* @param operation A function that outputs a Promise result. Typically, the operation does not use its arguments.
|
|
343
|
+
* @param backoff An array of retry backoff periods (in milliseconds) or a function for calculating them.
|
|
344
|
+
* @param shouldRetry A predicate function for deciding whether another call to the operation should occur.
|
|
345
|
+
* @returns A promise of the operation result, potentially with retries applied.
|
|
232
346
|
*/
|
|
233
347
|
export declare const withRetry: typeof PromiseUtils.withRetry;
|
|
234
348
|
/**
|
|
235
|
-
*
|
|
349
|
+
* Executes multiple jobs/operations with a specified level of concurrency.
|
|
350
|
+
*
|
|
351
|
+
* @param concurrency The number of jobs/operations to run concurrently.
|
|
352
|
+
* @param jobs The job data to be processed. This function can handle an infinite or unknown number of elements safely.
|
|
353
|
+
* @param operation The function that processes job data asynchronously.
|
|
354
|
+
* @returns A promise that resolves to an array containing the results from the operation function.
|
|
355
|
+
* The results in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
236
356
|
*/
|
|
237
357
|
export declare const withConcurrency: typeof PromiseUtils.withConcurrency;
|
|
238
358
|
/**
|
|
239
|
-
*
|
|
359
|
+
* Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures.
|
|
360
|
+
* In most cases, using withConcurrency might be more convenient.
|
|
361
|
+
*
|
|
362
|
+
* By default, this function does not throw or reject an error when any job/operation fails.
|
|
363
|
+
* Errors from operations are returned alongside results in the returned array.
|
|
364
|
+
* This function only resolves when all jobs/operations are settled (either resolved or rejected).
|
|
365
|
+
*
|
|
366
|
+
* If options.abortOnError is set to true, this function throws (or rejects with) an error immediately when any job/operation fails.
|
|
367
|
+
* In this mode, some jobs/operations may not be executed if one fails.
|
|
368
|
+
*
|
|
369
|
+
* @param parallelism The number of jobs/operations to run concurrently.
|
|
370
|
+
* @param jobs The job data to be processed. This function can safely handle an infinite or unknown number of elements.
|
|
371
|
+
* @param operation The function that processes job data asynchronously.
|
|
372
|
+
* @param options Options to control the function's behavior.
|
|
373
|
+
* @param options.abortOnError If true, the function aborts and throws an error on the first failed operation.
|
|
374
|
+
* @returns A promise that resolves to an array containing the results of the operations.
|
|
375
|
+
* Each element is either a fulfilled result or a rejected error/reason.
|
|
376
|
+
* The results or errors in the returned array are in the same order as the corresponding elements in the jobs array.
|
|
240
377
|
*/
|
|
241
378
|
export declare const inParallel: typeof PromiseUtils.inParallel;
|
|
242
379
|
/**
|
|
243
|
-
*
|
|
380
|
+
* Creates a Promise that resolves after a specified number of milliseconds.
|
|
381
|
+
*
|
|
382
|
+
* The result argument may be:
|
|
383
|
+
* - a value to resolve with,
|
|
384
|
+
* - a PromiseLike whose resolution will be adopted by the returned Promise, or
|
|
385
|
+
* - a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
386
|
+
*
|
|
387
|
+
* If result is a function, it is called when the timer elapses; if it returns a Promise,
|
|
388
|
+
* the returned Promise will adopt that Promise's outcome.
|
|
389
|
+
*
|
|
390
|
+
* @param ms The number of milliseconds after which the created Promise will resolve.
|
|
391
|
+
* @param result The result to be resolved by the Promise, or a function that supplies the result.
|
|
392
|
+
* @returns A Promise that resolves with the specified result after the specified delay.
|
|
244
393
|
*/
|
|
245
394
|
export declare const delayedResolve: typeof PromiseUtils.delayedResolve;
|
|
246
395
|
/**
|
|
247
|
-
*
|
|
396
|
+
* Creates a Promise that rejects after a specified number of milliseconds.
|
|
397
|
+
*
|
|
398
|
+
* The reason argument may be:
|
|
399
|
+
* - a value to reject with,
|
|
400
|
+
* - a PromiseLike whose rejection will be adopted by the returned Promise, or
|
|
401
|
+
* - a function which is invoked when the timer fires and may return a value or a PromiseLike.
|
|
402
|
+
*
|
|
403
|
+
* If reason is a function, it is called when the timer elapses; if it returns a Promise,
|
|
404
|
+
* the returned Promise will reject with that Promise's rejection reason (or reject with the
|
|
405
|
+
* returned value if it resolves).
|
|
406
|
+
*
|
|
407
|
+
* @param ms The number of milliseconds after which the created Promise will reject.
|
|
408
|
+
* @param reason The reason for the rejection, or a function that supplies the reason.
|
|
409
|
+
* @returns A Promise that rejects with the specified reason after the specified delay.
|
|
248
410
|
*/
|
|
249
411
|
export declare const delayedReject: typeof PromiseUtils.delayedReject;
|
|
250
412
|
/**
|
|
251
|
-
*
|
|
413
|
+
* Creates a cancellable timer that will resolve after a specified number of milliseconds.
|
|
414
|
+
*
|
|
415
|
+
* The returned object contains:
|
|
416
|
+
* - stop() to cancel the scheduled resolution (if called before the timer fires). Calling
|
|
417
|
+
* stop() prevents the promise from being settled by this timer.
|
|
418
|
+
* - promise which will resolve with the supplied result (or the value returned by the
|
|
419
|
+
* result function) after ms milliseconds unless stop() is called first.
|
|
420
|
+
*
|
|
421
|
+
* If the result is a PromiseLike, its resolution value will be used as the resolved value.
|
|
422
|
+
*
|
|
423
|
+
* @param ms The number of milliseconds after which the scheduled resolution will occur.
|
|
424
|
+
* @param result The result to be resolved by the Promise, or a function that supplies the result.
|
|
425
|
+
* @returns An object with stop() and promise.
|
|
426
|
+
*/
|
|
427
|
+
export declare const cancellableDelayedResolve: typeof PromiseUtils.cancellableDelayedResolve;
|
|
428
|
+
/**
|
|
429
|
+
* Creates a cancellable timer that will reject after a specified number of milliseconds.
|
|
430
|
+
*
|
|
431
|
+
* The returned object contains:
|
|
432
|
+
* - stop() to cancel the scheduled rejection (if called before the timer fires). Calling
|
|
433
|
+
* stop() prevents the promise from being settled by this timer.
|
|
434
|
+
* - promise which will reject with the supplied reason (or the value returned by the
|
|
435
|
+
* reason function) after ms milliseconds unless stop() is called first.
|
|
436
|
+
*
|
|
437
|
+
* If the reason is a PromiseLike that rejects, its rejection value will be used as the rejection reason.
|
|
438
|
+
*
|
|
439
|
+
* @param ms The number of milliseconds after which the scheduled rejection will occur.
|
|
440
|
+
* @param reason The reason for the rejection, or a function that supplies the reason.
|
|
441
|
+
* @returns An object with stop() and promise.
|
|
442
|
+
*/
|
|
443
|
+
export declare const cancellableDelayedReject: typeof PromiseUtils.cancellableDelayedReject;
|
|
444
|
+
/**
|
|
445
|
+
* Applies a timeout to a Promise or a function that returns a Promise.
|
|
446
|
+
* If the timeout occurs, the returned Promise resolves to the specified result.
|
|
447
|
+
* If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
|
|
448
|
+
* If the result parameter is a function and the timeout does not occur, the function will not be called.
|
|
449
|
+
* Note: The rejection of the operation parameter is not handled by this function.
|
|
450
|
+
* You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
451
|
+
*
|
|
452
|
+
* @param operation The original Promise or a function that returns a Promise to which the timeout will be applied.
|
|
453
|
+
* @param ms The number of milliseconds for the timeout.
|
|
454
|
+
* @param result The result to resolve with if the timeout occurs, or a function that supplies the result.
|
|
455
|
+
* @returns A new Promise that resolves to the specified result if the timeout occurs.
|
|
252
456
|
*/
|
|
253
457
|
export declare const timeoutResolve: typeof PromiseUtils.timeoutResolve;
|
|
254
458
|
/**
|
|
255
|
-
*
|
|
459
|
+
* Applies a timeout to a Promise or a function that returns a Promise.
|
|
460
|
+
* If the timeout occurs, the returned Promise rejects with the specified reason.
|
|
461
|
+
* If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise.
|
|
462
|
+
* If the rejectReason parameter is a function and the timeout does not occur, the function will not be called.
|
|
463
|
+
* Note: The rejection of the operation parameter is not handled by this function. You may want to handle it outside this function to avoid warnings like "(node:4330) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously."
|
|
464
|
+
*
|
|
465
|
+
* @param operation The original Promise or a function that returns a Promise to which the timeout will be applied.
|
|
466
|
+
* @param ms The number of milliseconds for the timeout.
|
|
467
|
+
* @param rejectReason The reason to reject with if the timeout occurs, or a function that supplies the reason.
|
|
468
|
+
* @returns A new Promise that rejects with the specified reason if the timeout occurs.
|
|
256
469
|
*/
|
|
257
470
|
export declare const timeoutReject: typeof PromiseUtils.timeoutReject;
|
|
258
471
|
/**
|
|
259
|
-
*
|
|
472
|
+
* Provides mutual exclusion similar to synchronized in Java.
|
|
473
|
+
* Ensures no concurrent execution of any operation function associated with the same lock.
|
|
474
|
+
* The operation function has access to the state (when synchronized is called),
|
|
475
|
+
* settledState (when the operation function is called),
|
|
476
|
+
* and result (either the fulfilled result or the rejected reason) of the previous operation.
|
|
477
|
+
* If there is no previous invocation, state, settledState, and result will all be undefined.
|
|
478
|
+
*
|
|
479
|
+
* @param lock The object (such as a string, a number, or this in a class) used to identify the lock.
|
|
480
|
+
* @param operation The function that performs the computation and returns a Promise.
|
|
481
|
+
* @returns The result of the operation function.
|
|
260
482
|
*/
|
|
261
483
|
export declare const synchronized: typeof PromiseUtils.synchronized;
|
|
262
484
|
/**
|
|
263
|
-
*
|
|
485
|
+
* This is just another spelling of synchronized.
|
|
486
|
+
* @param lock The object (such as a string, a number, or this in a class) used to identify the lock.
|
|
487
|
+
* @param operation The function that performs the computation and returns a Promise.
|
|
488
|
+
* @returns The result of the operation function.
|
|
264
489
|
*/
|
|
265
490
|
export declare const synchronised: typeof PromiseUtils.synchronised;
|
|
266
491
|
/**
|
|
267
|
-
*
|
|
492
|
+
* Retrieves the state of the specified Promise.
|
|
493
|
+
* Note: The returned value is a Promise that resolves immediately.
|
|
494
|
+
*
|
|
495
|
+
* @param p The Promise whose state is to be determined.
|
|
496
|
+
* @returns A Promise that resolves immediately with the state of the input Promise.
|
|
268
497
|
*/
|
|
269
498
|
export declare const promiseState: typeof PromiseUtils.promiseState;
|
|
499
|
+
/**
|
|
500
|
+
* Runs an operation periodically with configurable intervals and stopping conditions.
|
|
501
|
+
*
|
|
502
|
+
* - `interval` may be a single number (ms), an array of numbers, or a function
|
|
503
|
+
* that receives the iteration number (starting at 1) and returns the next
|
|
504
|
+
* interval in milliseconds or `undefined` to stop.
|
|
505
|
+
* - If the interval array runs out of elements or the function returns `undefined`
|
|
506
|
+
* (or a negative value), no further invocations will be scheduled.
|
|
507
|
+
*
|
|
508
|
+
* Options:
|
|
509
|
+
* - `maxExecutions` stop after N runs (inclusive).
|
|
510
|
+
* - `maxDurationMs` stop after elapsed ms since the first scheduled start.
|
|
511
|
+
* - `schedule` controls how the interval is measured:
|
|
512
|
+
* - `'delayAfterEnd'`: wait the interval after the previous operation completes
|
|
513
|
+
* before scheduling the next one (equivalent to a fixed delay between ends).
|
|
514
|
+
* - `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured
|
|
515
|
+
* between the starts of successive operations).
|
|
516
|
+
* The default schedule is `'delayBetweenStarts'`.
|
|
517
|
+
*
|
|
518
|
+
* Returns an object with `stop()` to cancel further executions and `done` which
|
|
519
|
+
* resolves when the periodic runner stops. If the provided `operation` throws or
|
|
520
|
+
* rejects, the `done` promise will reject with that error so callers can handle it.
|
|
521
|
+
*
|
|
522
|
+
* Note: The first invocation of `operation` is scheduled after the first interval
|
|
523
|
+
* elapses (i.e. this function does NOT call `operation` immediately). If you need
|
|
524
|
+
* an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`.
|
|
525
|
+
*
|
|
526
|
+
* @template T The operation return type (ignored by the runner; used for typing).
|
|
527
|
+
* @param operation Function to run each iteration. Receives the iteration index (1-based).
|
|
528
|
+
* @param interval Number | number[] | ((iteration: number) => number|undefined) defining waits.
|
|
529
|
+
* @param options Optional configuration.
|
|
530
|
+
* @param options.maxExecutions Stop after N executions.
|
|
531
|
+
* @param options.maxDurationMs Stop after N milliseconds.
|
|
532
|
+
* @param options.schedule How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`.
|
|
533
|
+
* @returns An object containing `stop()` to cancel further executions and `done` Promise
|
|
534
|
+
* which resolves when the periodic runner stops (or rejects if the operation errors).
|
|
535
|
+
*/
|
|
536
|
+
export declare const runPeriodically: typeof PromiseUtils.runPeriodically;
|
|
270
537
|
//# sourceMappingURL=promise-utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-utils.d.ts","sourceRoot":"","sources":["../src/promise-utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,UAAkJ,CAAC;AAElL;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,UAA6K,CAAC;AAE/M;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"promise-utils.d.ts","sourceRoot":"","sources":["../src/promise-utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,UAAkJ,CAAC;AAElL;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,UAA6K,CAAC;AAE/M;;GAEG;AACH,eAAO,MAAM,YAAY;;;;CAIf,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAAG,MAAM,OAAO,YAAY,CAAC;AAEzD,8BAAsB,YAAY;IAChC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;WACU,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,UAAU,EAC3C,SAAS,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,EACzD,aAAa,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAA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