/** * Array of 25 Fibonacci numbers starting from 1 up to 317811. * It can be used to form your own backoff interval array. * @example * // 1ms, 2ms, 3ms, 5ms, 8ms, 13ms * PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5), err => err.statusCode === 429); * // 1s, 2s, 3s, 4s, 8s, 10s, 10s, 10s, 10s, 10s * PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10)), err => err.statusCode === 429); * // with +-10% randomness: 1s, 2s, 3s, 5s, 8s, 13s * PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 5).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429); */ export declare const FIBONACCI_SEQUENCE: number[]; /** * Array of 25 exponential numbers starting from 1 up to 33554432. * It can be used to form your own backoff interval array. * @example * // 1ms, 2ms, 4ms, 8ms, 16ms, 32ms * PromiseUtils.withRetry(() => doSomething(), EXPONENTIAL_SEQUENCE.slice(0, 5), err => err.statusCode === 429); * // 1s, 2s, 4s, 8s, 10s, 10s, 10s, 10s, 10s, 10s * PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(EXPONENTIAL_SEQUENCE[i], 10)), err => err.statusCode === 429); * // with +-10% randomness: 1s, 2s, 4s, 8s * PromiseUtils.withRetry(() => doSomething(), FIBONACCI_SEQUENCE.slice(0, 4).map(n => 1000 * n * (1 + (Math.random() - 0.5) / 5)), err => err.statusCode === 429); */ export declare const EXPONENTIAL_SEQUENCE: number[]; /** * The state of a Promise can only be one of: Pending, Fulfilled, and Rejected. */ export declare const PromiseState: { readonly Pending: "Pending"; readonly Fulfilled: "Fulfilled"; readonly Rejected: "Rejected"; }; export type PromiseStateType = keyof typeof PromiseState; export declare abstract class PromiseUtils { /** * Executes an operation repeatedly and collects all the results. * This function is very useful for many scenarios, such like client-side pagination. * * @example * const domainNameObjects = await PromiseUtils.repeat( * pagingParam => apig.getDomainNames({limit: 500, ...pagingParam}).promise(), * response => response.position? {position: response.position} : null, * (collection, response) => collection.concat(response.items!), * [] as APIGateway.DomainName[], * ); * * @template Result The type of the operation result. * @template Param The type of the input to the operation, typically a paging parameter. * @template Collection The type of the collection returned by this function. * * @param operation A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging. * @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. * @param collect A function for merging the operation result into the collection. * @param initialCollection The initial collection, which will be the first argument passed to the first invocation of the collect function. * @param initialParameter The parameter for the first operation. * @returns A promise that resolves to a collection of all the results returned by the operation function. * */ static repeat(operation: (parameter: Partial) => Promise, nextParameter: (response: Result) => Partial | Promise> | null, collect: (collection: Collection, result: Result) => Collection, initialCollection: Collection, initialParameter?: Partial): Promise; /** * Repeatedly performs an operation until a specified criteria is met. * * @example * const result = await PromiseUtils.withRetry(() => doSomething(), [100, 200, 300, 500, 800, 1000]); * const result2 = await PromiseUtils.withRetry(() => doSomething(), Array.from({length: 10}, (_v, i) => 1000 * Math.min(FIBONACCI_SEQUENCE[i], 10), err => err.statusCode === 429); * const result3 = await PromiseUtils.withRetry(() => doSomething(), attempt => attempt <= 8 ? 1000 * Math.min(FIBONACCI_SEQUENCE[attempt - 1], 10) : undefined, err => err.statusCode === 429); * * @template Result Type of the operation result. * @template TError Type of the possible error that could be generated by the operation. * * @param operation A function that outputs a Promise result. Typically, the operation does not use its arguments. * @param backoff An array of retry backoff periods (in milliseconds) or a function for calculating them. * If retry is desired, the specified backoff period is waited before the next call to the operation. * If the array runs out of elements or the function returns `undefined` or a negative number, no further calls to the operation will be made. * The `attempt` argument passed to the backoff function starts from 1, as it is called immediately after the first attempt and before the first retry. * @param shouldRetry A predicate function for deciding whether another call to the operation should occur. * If this argument is not defined, a retry will occur whenever the operation rejects with an error. * The `shouldRetry` function is evaluated before the `backoff`. * The `attempt` argument passed to the shouldRetry function starts from 1. * @returns A promise of the operation result, potentially with retries applied. */ static withRetry(operation: (attempt: number, previousResult: Result | undefined, previousError: TError | undefined) => Promise, backoff: Array | ((attempt: number, previousResult: Result | undefined, previousError: TError | undefined) => number | undefined), shouldRetry?: (previousError: TError | undefined, previousResult: Result | undefined, attempt: number) => boolean): Promise; /** * Executes multiple jobs/operations with a specified level of concurrency. * * Unlike `inParallel(...)`, this function may throw or reject an error when a job/operation fails. * When an error is thrown, the function rejects immediately, and no further operations will be started. * If you want all the operations to always be executed, use {@link PromiseUtils.inParallel} instead. * * @example * // At any time, there would be no more than 5 concurrency API calls. Error would be re-thrown immediately when it occurs. * const attributes = await PromiseUtils.withConcurrency(5, topicArns, async (topicArn) => { * const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!; * return topicAttributes; * }); * * * @template Data The type of the job data, typically an Array. * @template Result The type of the return value from the operation function. * * @param concurrency The number of jobs/operations to run concurrently. * @param jobs The job data to be processed. This function can handle an infinite or unknown number of elements safely. * @param operation The function that processes job data asynchronously. * @returns A promise that resolves to an array containing the results from the operation function. * The results in the returned array are in the same order as the corresponding elements in the jobs array. */ static withConcurrency(concurrency: number, jobs: Iterable, operation: (job: Data, index: number) => Promise): Promise>; /** * Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures. * In most cases, using {@link PromiseUtils.withConcurrency} might be more convenient. * * By default, this function does not throw or reject an error when any job/operation fails. * Errors from operations are returned alongside results in the returned array. * This function only resolves when all jobs/operations are settled (either resolved or rejected). * * If `options.abortOnError` is set to true, this function throws (or rejects with) an error immediately when any job/operation fails. * In this mode, no further operations will be started after a failure occurs. * * @example * // Capture errors in the returned array * const attributesAndPossibleErrors: Array = await PromiseUtils.inParallel(5, topicArns, async (topicArn) => { * const topicAttributes = (await sns.getTopicAttributes({ TopicArn: topicArn }).promise()).Attributes!; * return topicAttributes; * }); * * // Abort on the first error * let results: Array; * try { * results = await PromiseUtils.inParallel(100, jobs, async (job) => processor.process(job), { abortOnError: true }); * } catch (error) { * // handle the error * } * * @template Data The type of the job data, typically an Array. * @template Result The type of the return value from the operation function. * @template TError The type for the error that could be thrown from the operation function, defaults to `Result`. * * @param parallelism The number of jobs/operations to run concurrently. * @param jobs The job data to be processed. This function can safely handle an infinite or unknown number of elements. * @param operation The function that processes job data asynchronously. * @param options Options to control the function's behavior. * @param options.abortOnError If true, the function aborts and throws an error on the first failed operation. * @returns A promise that resolves to an array containing the results of the operations. * Each element is either a fulfilled result or a rejected error/reason. * The results or errors in the returned array are in the same order as the corresponding elements in the jobs array. */ static inParallel(parallelism: number, jobs: Iterable, operation: (job: Data, index: number) => Promise, options?: { abortOnError: boolean; }): Promise>; /** * Creates a cancellable timer that will resolve after a specified number of milliseconds. * * The returned object contains: * - `stop()` to cancel the scheduled resolution (if called before the timer fires). Calling * `stop()` prevents the promise from being settled by this timer. * - `promise` which will resolve with the supplied `result` (or the value returned by the * `result` function) after `ms` milliseconds unless `stop()` is called first. * * Note: If the `result` is a function that returns a Promise, the returned `promise` will * resolve with that Promise's resolution (i.e. it behaves like resolving with a PromiseLike). * * @param ms The number of milliseconds after which the scheduled resolution will occur. * @param result The result to be resolved by the Promise, or a function that supplies the result. * @returns An object with `stop()` and `promise`. */ static cancellableDelayedResolve(ms: number, result?: T | PromiseLike | (() => (T | PromiseLike))): { stop: () => void; promise: Promise; }; /** * Creates a Promise that resolves after a specified number of milliseconds. * * The `result` argument may be: * - a value to resolve with, * - a PromiseLike whose resolution will be adopted by the returned Promise, or * - a function which is invoked when the timer fires and may return a value or a PromiseLike. * * If `result` is a function, it is called when the timer elapses; if it returns a Promise, * the returned Promise will adopt that Promise's outcome. * * @param ms The number of milliseconds after which the created Promise will resolve. * @param result The result to be resolved by the Promise, or a function that supplies the result. * @returns A Promise that resolves with the specified result after the specified delay. */ static delayedResolve(ms: number, result?: T | PromiseLike | (() => (T | PromiseLike))): Promise; /** * Creates a cancellable timer that will reject after a specified number of milliseconds. * * The returned object contains: * - `stop()` to cancel the scheduled rejection (if called before the timer fires). Calling * `stop()` prevents the promise from being settled by this timer. * - `promise` which will reject with the supplied `reason` (or the value returned by the * `reason` function) after `ms` milliseconds unless `stop()` is called first. * * If the `reason` is a PromiseLike that rejects, its rejection value will be used as the rejection reason. * * @param ms The number of milliseconds after which the scheduled rejection will occur. * @param reason The reason for the rejection, or a function that supplies the reason. * @returns An object with `stop()` and `promise`. */ static cancellableDelayedReject(ms: number, reason: R | PromiseLike | (() => R | PromiseLike)): { stop: () => void; promise: Promise; }; /** * Creates a Promise that rejects after a specified number of milliseconds. * * The `reason` argument may be: * - a value to reject with, * - a PromiseLike whose rejection will be adopted by the returned Promise, or * - a function which is invoked when the timer fires and may return a value or a PromiseLike. * * If `reason` is a function, it is called when the timer elapses; if it returns a Promise, * the returned Promise will reject with that Promise's rejection reason (or reject with the * returned value if it resolves). * * @param ms The number of milliseconds after which the created Promise will reject. * @param reason The reason for the rejection, or a function that supplies the reason. * @returns A Promise that rejects with the specified reason after the specified delay. */ static delayedReject(ms: number, reason: R | PromiseLike | (() => R | PromiseLike)): Promise; /** * Applies a timeout to a Promise or a function that returns a Promise. * If the timeout occurs, the returned Promise resolves to the specified result. * If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise. * If the `result` parameter is a function and the timeout does not occur, the function will not be called. * 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." * * @param operation The original Promise or a function that returns a Promise to which the timeout will be applied. * @param ms The number of milliseconds for the timeout. * @param result The result to resolve with if the timeout occurs, or a function that supplies the result. * @returns A new Promise that resolves to the specified result if the timeout occurs. */ static timeoutResolve(operation: Promise | (() => Promise), ms: number, result?: T | PromiseLike | (() => (T | PromiseLike)) | undefined): Promise; /** * Applies a timeout to a Promise or a function that returns a Promise. * If the timeout occurs, the returned Promise rejects with the specified reason. * If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise. * If the `rejectReason` parameter is a function and the timeout does not occur, the function will not be called. * 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." * * @param operation The original Promise or a function that returns a Promise to which the timeout will be applied. * @param ms The number of milliseconds for the timeout. * @param rejectReason The reason to reject with if the timeout occurs, or a function that supplies the reason. * @returns A new Promise that rejects with the specified reason if the timeout occurs. */ static timeoutReject(operation: Promise | (() => Promise), ms: number, rejectReason: R | PromiseLike | (() => R | PromiseLike)): Promise; /** * Retrieves the state of the specified Promise. * Note: The returned value is a Promise that resolves immediately. * * @param p The Promise whose state is to be determined. * @returns A Promise that resolves immediately with the state of the input Promise. */ static promiseState(p: Promise): Promise; private static synchronizationLocks; /** * Provides mutual exclusion similar to `synchronized` in Java. * Ensures no concurrent execution of any operation function associated with the same lock. * The operation function has access to the state (when `synchronized` is called), * settledState (when the operation function is called), * and result (either the fulfilled result or the rejected reason) of the previous operation. * If there is no previous invocation, state, settledState, and result will all be undefined. * * @param lock The object (such as a string, a number, or `this` in a class) used to identify the lock. * @param operation The function that performs the computation and returns a Promise. * @returns The result of the operation function. */ static synchronized(lock: any, operation: (previousState: PromiseStateType | undefined, previousSettledState: PromiseStateType | undefined, previousResult: any) => Promise): Promise; /** * This is just another spelling of {@link PromiseUtils.synchronized}. * @param lock The object (such as a string, a number, or `this` in a class) used to identify the lock. * @param operation The function that performs the computation and returns a Promise. * @returns The result of the operation function. */ static synchronised(lock: any, operation: (previousState: PromiseStateType | undefined, previousSettledState: PromiseStateType | undefined, previousResult: any) => Promise): Promise; /** * Runs an operation periodically with configurable intervals and stopping conditions. * * - `interval` may be a single number (ms), an array of numbers, or a function * that receives the iteration number (starting at 1) and returns the next * interval in milliseconds or `undefined` to stop. * - If the interval array runs out of elements or the function returns `undefined` * (or a negative value), no further invocations will be scheduled. * * Options: * - `maxExecutions` stop after N runs (inclusive). * - `maxDurationMs` stop after elapsed ms since the first scheduled start. * - `schedule` controls how the interval is measured: * - `'delayAfterEnd'`: wait the interval after the previous operation completes * before scheduling the next one (equivalent to a fixed delay between ends). * - `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured * between the starts of successive operations). * The default schedule is `'delayBetweenStarts'`. * * Returns an object with `stop()` to cancel further executions and `done` which * resolves when the periodic runner stops. If the provided `operation` throws or * rejects, the `done` promise will reject with that error so callers can handle it. * * Note: The first invocation of `operation` is scheduled after the first interval * elapses (i.e. this function does NOT call `operation` immediately). If you need * an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`. * * @template T The operation return type (ignored by the runner; used for typing). * @param operation Function to run each iteration. Receives the iteration index (1-based). * @param interval Number | number[] | ((iteration: number) => number|undefined) defining waits. * @param options Optional configuration. * @param options.maxExecutions Stop after N executions. * @param options.maxDurationMs Stop after N milliseconds. * @param options.schedule How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`. * @returns An object containing `stop()` to cancel further executions and `done` Promise * which resolves when the periodic runner stops (or rejects if the operation errors). */ static runPeriodically(operation: (iteration: number) => Promise | T, interval: number | Array | ((iteration: number) => number | undefined), options?: { maxExecutions?: number; maxDurationMs?: number; schedule?: 'delayAfterEnd' | 'delayBetweenStarts'; }): { stop: () => void; done: Promise; }; } /** * Executes an operation repeatedly and collects all the results. * This function is very useful for many scenarios, such like client-side pagination. * * @param operation A function that takes a parameter as input and returns a result. Typically, the parameter has optional fields to control paging. * @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. * @param collect A function for merging the operation result into the collection. * @param initialCollection The initial collection, which will be the first argument passed to the first invocation of the collect function. * @param initialParameter The parameter for the first operation. * @returns A promise that resolves to a collection of all the results returned by the operation function. */ export declare const repeat: typeof PromiseUtils.repeat; /** * Repeatedly performs an operation until a specified criteria is met. * * @param operation A function that outputs a Promise result. Typically, the operation does not use its arguments. * @param backoff An array of retry backoff periods (in milliseconds) or a function for calculating them. * @param shouldRetry A predicate function for deciding whether another call to the operation should occur. * @returns A promise of the operation result, potentially with retries applied. */ export declare const withRetry: typeof PromiseUtils.withRetry; /** * Executes multiple jobs/operations with a specified level of concurrency. * * @param concurrency The number of jobs/operations to run concurrently. * @param jobs The job data to be processed. This function can handle an infinite or unknown number of elements safely. * @param operation The function that processes job data asynchronously. * @returns A promise that resolves to an array containing the results from the operation function. * The results in the returned array are in the same order as the corresponding elements in the jobs array. */ export declare const withConcurrency: typeof PromiseUtils.withConcurrency; /** * Executes multiple jobs/operations in parallel. By default, all operations are executed regardless of any failures. * In most cases, using withConcurrency might be more convenient. * * By default, this function does not throw or reject an error when any job/operation fails. * Errors from operations are returned alongside results in the returned array. * This function only resolves when all jobs/operations are settled (either resolved or rejected). * * If options.abortOnError is set to true, this function throws (or rejects with) an error immediately when any job/operation fails. * In this mode, no further operations will be started after a failure occurs. * * @param parallelism The number of jobs/operations to run concurrently. * @param jobs The job data to be processed. This function can safely handle an infinite or unknown number of elements. * @param operation The function that processes job data asynchronously. * @param options Options to control the function's behavior. * @param options.abortOnError If true, the function aborts and throws an error on the first failed operation. * @returns A promise that resolves to an array containing the results of the operations. * Each element is either a fulfilled result or a rejected error/reason. * The results or errors in the returned array are in the same order as the corresponding elements in the jobs array. */ export declare const inParallel: typeof PromiseUtils.inParallel; /** * Creates a Promise that resolves after a specified number of milliseconds. * * The result argument may be: * - a value to resolve with, * - a PromiseLike whose resolution will be adopted by the returned Promise, or * - a function which is invoked when the timer fires and may return a value or a PromiseLike. * * If result is a function, it is called when the timer elapses; if it returns a Promise, * the returned Promise will adopt that Promise's outcome. * * @param ms The number of milliseconds after which the created Promise will resolve. * @param result The result to be resolved by the Promise, or a function that supplies the result. * @returns A Promise that resolves with the specified result after the specified delay. */ export declare const delayedResolve: typeof PromiseUtils.delayedResolve; /** * Creates a Promise that rejects after a specified number of milliseconds. * * The reason argument may be: * - a value to reject with, * - a PromiseLike whose rejection will be adopted by the returned Promise, or * - a function which is invoked when the timer fires and may return a value or a PromiseLike. * * If reason is a function, it is called when the timer elapses; if it returns a Promise, * the returned Promise will reject with that Promise's rejection reason (or reject with the * returned value if it resolves). * * @param ms The number of milliseconds after which the created Promise will reject. * @param reason The reason for the rejection, or a function that supplies the reason. * @returns A Promise that rejects with the specified reason after the specified delay. */ export declare const delayedReject: typeof PromiseUtils.delayedReject; /** * Creates a cancellable timer that will resolve after a specified number of milliseconds. * * The returned object contains: * - stop() to cancel the scheduled resolution (if called before the timer fires). Calling * stop() prevents the promise from being settled by this timer. * - promise which will resolve with the supplied result (or the value returned by the * result function) after ms milliseconds unless stop() is called first. * * If the result is a PromiseLike, its resolution value will be used as the resolved value. * * @param ms The number of milliseconds after which the scheduled resolution will occur. * @param result The result to be resolved by the Promise, or a function that supplies the result. * @returns An object with stop() and promise. */ export declare const cancellableDelayedResolve: typeof PromiseUtils.cancellableDelayedResolve; /** * Creates a cancellable timer that will reject after a specified number of milliseconds. * * The returned object contains: * - stop() to cancel the scheduled rejection (if called before the timer fires). Calling * stop() prevents the promise from being settled by this timer. * - promise which will reject with the supplied reason (or the value returned by the * reason function) after ms milliseconds unless stop() is called first. * * If the reason is a PromiseLike that rejects, its rejection value will be used as the rejection reason. * * @param ms The number of milliseconds after which the scheduled rejection will occur. * @param reason The reason for the rejection, or a function that supplies the reason. * @returns An object with stop() and promise. */ export declare const cancellableDelayedReject: typeof PromiseUtils.cancellableDelayedReject; /** * Applies a timeout to a Promise or a function that returns a Promise. * If the timeout occurs, the returned Promise resolves to the specified result. * If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise. * If the result parameter is a function and the timeout does not occur, the function will not be called. * 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." * * @param operation The original Promise or a function that returns a Promise to which the timeout will be applied. * @param ms The number of milliseconds for the timeout. * @param result The result to resolve with if the timeout occurs, or a function that supplies the result. * @returns A new Promise that resolves to the specified result if the timeout occurs. */ export declare const timeoutResolve: typeof PromiseUtils.timeoutResolve; /** * Applies a timeout to a Promise or a function that returns a Promise. * If the timeout occurs, the returned Promise rejects with the specified reason. * If the timeout does not occur, the returned Promise resolves or rejects based on the outcome of the original Promise. * If the rejectReason parameter is a function and the timeout does not occur, the function will not be called. * 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." * * @param operation The original Promise or a function that returns a Promise to which the timeout will be applied. * @param ms The number of milliseconds for the timeout. * @param rejectReason The reason to reject with if the timeout occurs, or a function that supplies the reason. * @returns A new Promise that rejects with the specified reason if the timeout occurs. */ export declare const timeoutReject: typeof PromiseUtils.timeoutReject; /** * Provides mutual exclusion similar to synchronized in Java. * Ensures no concurrent execution of any operation function associated with the same lock. * The operation function has access to the state (when synchronized is called), * settledState (when the operation function is called), * and result (either the fulfilled result or the rejected reason) of the previous operation. * If there is no previous invocation, state, settledState, and result will all be undefined. * * @param lock The object (such as a string, a number, or this in a class) used to identify the lock. * @param operation The function that performs the computation and returns a Promise. * @returns The result of the operation function. */ export declare const synchronized: typeof PromiseUtils.synchronized; /** * This is just another spelling of synchronized. * @param lock The object (such as a string, a number, or this in a class) used to identify the lock. * @param operation The function that performs the computation and returns a Promise. * @returns The result of the operation function. */ export declare const synchronised: typeof PromiseUtils.synchronised; /** * Retrieves the state of the specified Promise. * Note: The returned value is a Promise that resolves immediately. * * @param p The Promise whose state is to be determined. * @returns A Promise that resolves immediately with the state of the input Promise. */ export declare const promiseState: typeof PromiseUtils.promiseState; /** * Runs an operation periodically with configurable intervals and stopping conditions. * * - `interval` may be a single number (ms), an array of numbers, or a function * that receives the iteration number (starting at 1) and returns the next * interval in milliseconds or `undefined` to stop. * - If the interval array runs out of elements or the function returns `undefined` * (or a negative value), no further invocations will be scheduled. * * Options: * - `maxExecutions` stop after N runs (inclusive). * - `maxDurationMs` stop after elapsed ms since the first scheduled start. * - `schedule` controls how the interval is measured: * - `'delayAfterEnd'`: wait the interval after the previous operation completes * before scheduling the next one (equivalent to a fixed delay between ends). * - `'delayBetweenStarts'`: keep start times on a regular schedule (interval measured * between the starts of successive operations). * The default schedule is `'delayBetweenStarts'`. * * Returns an object with `stop()` to cancel further executions and `done` which * resolves when the periodic runner stops. If the provided `operation` throws or * rejects, the `done` promise will reject with that error so callers can handle it. * * Note: The first invocation of `operation` is scheduled after the first interval * elapses (i.e. this function does NOT call `operation` immediately). If you need * an immediate run, invoke `operation(1)` yourself before calling `runPeriodically`. * * @template T The operation return type (ignored by the runner; used for typing). * @param operation Function to run each iteration. Receives the iteration index (1-based). * @param interval Number | number[] | ((iteration: number) => number|undefined) defining waits. * @param options Optional configuration. * @param options.maxExecutions Stop after N executions. * @param options.maxDurationMs Stop after N milliseconds. * @param options.schedule How to measure intervals: `'delayAfterEnd'` or `'delayBetweenStarts'`. * @returns An object containing `stop()` to cancel further executions and `done` Promise * which resolves when the periodic runner stops (or rejects if the operation errors). */ export declare const runPeriodically: typeof PromiseUtils.runPeriodically; //# sourceMappingURL=promise-utils.d.ts.map