fcr-core
Version:
Core APIs for building online scenes
39 lines (38 loc) • 1.35 kB
TypeScript
export type RetryOptions = {
retriesMax: number;
};
/**
* Creates an abortable retry operation that can be cancelled at any time.
*
* The handler function will be retried up to `retriesMax` times if it fails.
* The operation can be cancelled by calling the returned abort function,
* which will cause the promise to reject with an AbortError.
*
* @example
* ```typescript
* const [abort, promise] = abortableRetry(async (signal) => {
* await asyncFunction1();
* // Check if aborted after each async operation
* signal.throwIfAborted();
*
* await asyncFunction2();
* signal.throwIfAborted();
*
* return result;
* }, { retriesMax: 10 });
*
* // Cancel the operation at any time
* setTimeout(() => abort(), 5000);
*
* const [err, result] = await promise;
* if (err?.name === 'AbortError') {
* console.log('Operation was cancelled');
* }
* ```
*
* @param handler - Async function that receives an AbortSignal.
* Should call `signal.throwIfAborted()` periodically to check for cancellation.
* @param options - Retry configuration
* @returns Tuple of [abort function, result promise]. The promise resolves to [error, result].
*/
export declare const abortableRetry: <T>(handler: (signal: AbortSignal) => Promise<T>, options: RetryOptions) => readonly [() => void, Promise<[Error | null, T]>];