UNPKG

@react-hive/honey-utils

Version:

A lightweight TypeScript utility library providing a collection of helper functions for common programming tasks

113 lines (112 loc) 3.7 kB
export declare const noop: () => void; /** * Invokes the given input if it is a function, passing the provided arguments. * Otherwise, returns the input as-is. * * @template Args - Tuple of argument types to pass to the function. * @template Result - Return type of the function or the value. * * @param input - A function to invoke with `args`, or a direct value of type `Result`. * @param args - Arguments to pass if `input` is a function. * * @returns The result of invoking the function, or the original value if it's not a function. */ export declare const invokeIfFunction: <Args extends unknown[], Result>(input: ((...args: Args) => Result) | Result, ...args: Args) => Result; /** * Creates a promise that resolves after the specified delay. * * Useful for creating artificial delays, implementing timeouts, or spacing operations. * * @param delayMs - The delay in milliseconds. * * @returns A promise that resolves after the specified delay. * * @example * ```ts * // Wait for 1 second * await delay(1000); * console.log('This logs after 1 second'); * * // Use with other async operations * async function fetchWithTimeout() { * const timeoutPromise = delay(5000).then(() => { * throw new Error('Request timed out'); * }); * * return Promise.race([fetchData(), timeoutPromise]); * } * ``` */ export declare const delay: (delayMs: number) => Promise<void>; interface RetryOptions { /** * Maximum number of retry attempts before failing. * * @default 3 */ maxAttempts?: number; /** * Delay in milliseconds between retry attempts. * If `backoff` is true, this is the base delay for exponential backoff. * * @default 300 */ delayMs?: number; /** * Whether to use exponential backoff for delays between attempts. * When enabled, the delay is multiplied by 2 ^ (`attempt` - 1). * * @default true */ backoff?: boolean; /** * Optional callback triggered before each retry attempt. * * @param attempt - The current attempt number (starting from 1). * @param error - The error that caused the retry. */ onRetry?: (attempt: number, error: unknown) => void; } /** * Wraps an asynchronous function with retry logic. * * The returned function will attempt to call the original function up to `maxAttempts` times, * with a delay between retries. If all attempts fail, the last encountered error is thrown. * * Useful for operations that may fail intermittently, such as network requests. * * @template Task - The type of the async function to wrap. * @template TaskResult - The result type of the async function. * * @param task - The async function to wrap with retry logic. * @param options - Configuration options for retry behavior. * * @returns A function that wraps the original function with retry support. * * @example * ```ts * async function fetchData() { * const response = await fetch('/api/data'); * * if (!response.ok) { * throw new Error('Network error'); * } * * return await response.json(); * } * * const fetchWithRetry = retry(fetchData, { * maxAttempts: 5, * delayMs: 500, * onRetry: (attempt, error) => { * console.warn(`Attempt ${attempt} failed:`, error); * } * }); * * fetchWithRetry() * .then(data => console.log('Success:', data)) * .catch(error => console.error('Failed after retries:', error)); * ``` */ export declare const retry: <Task extends (...args: unknown[]) => Promise<TaskResult>, TaskResult>(task: Task, { maxAttempts, delayMs, backoff, onRetry }?: RetryOptions) => ((...args: Parameters<Task>) => Promise<TaskResult>); export {};