async-wrappers
Version:
A set of wrapper functions to perform debouncing, throttling, retrying etc.
135 lines (134 loc) • 4.1 kB
TypeScript
/**
* Function for determining a how long to wait after the given attempt
*
* @category Retry
*/
declare type RetryDelayCallback =
/**
* @param attempt The current attempt number.
* @param previousDelay The previous delay.
* @param error The error from the current attempt.
* @return The new delay in milliseconds.
*/
(attempt: number, previousDelay: number, error: any) => number | PromiseLike<number>;
/**
* A delay, list of delays or [[RetryNextDelayFunction|callback]] for
* [[retry]] to use when determining the delay between attempts.
*
* @category Retry
*/
declare type RetryDelay = RetryDelayCallback | number[] | number;
/**
* A function used to determine if to stop for the given retry attempt.
*
* Errors thrown by this function will cause retry to reject with the
* errorTypes of retry errors
*
* @category Retry
*/
declare type RetryStopCallback =
/**
* @param nextAttempt The attempt number for the next try.
* @param delay The current delay before retrying.
* @param error The error from the current attempt.
* @return true or a message to stop trying and reject with the given
* message.
*/
(nextAttempt: number, delay: number, error: any) => boolean | string;
/**
* The number of calls or [[RetryStopCallback|callback]] for [[retry]] to use when determining
* to stop retrying.
*
* @category Retry
*/
declare type RetryStop = RetryStopCallback | number;
/**
* callback for [[retry]] to determine the arguments to
* the function to retry.
* @typeparam RetryFunction the type of function to retry.
*
* @category Retry
*/
export declare type RetryArgumentsCallback<RetryFunction extends (...args: any[]) => any> =
/**
* @param attempt The attempt number for the next try.
* @param delay The current delay before retrying.
* @param error The error from the current attempt.
* @return The arguments for the function
*/
(attempt: number, delay: number, error: any) => Parameters<RetryFunction> | Promise<Parameters<RetryFunction>>;
/**
* An array of arguments or [[RetryArgumentsCallback|callback]] for [[retry]]
* to use when calling it's function
*
* @category Retry
*/
export declare type RetryArguments<F extends (...args: any[]) => any> = Parameters<F> | RetryArgumentsCallback<F>;
/**
* Types of retry errors
*
* @category Retry
*/
export declare type RetryErrorTypes = 'cancelled' | 'stopped';
/**
* Base Class for all retry errors
*
* @category Retry
*/
export declare class RetryError extends Error {
/**
* The type of error
*/
type: RetryErrorTypes;
/**
* The last error that occurred when retrying
*/
error?: any;
constructor(type: RetryErrorTypes, message: string, error: any);
}
/**
* The error given when retrying is cancelled
*
* @category Retry
*/
export declare class RetryCancelledError extends RetryError {
constructor(message?: string, error?: any);
}
/**
* The error given when retrying stops
*
* @category Retry
*/
export declare class RetryStoppedError extends RetryError {
constructor(message?: string, error?: any);
}
/**
* The return type of [[retry]]
*
* @category Retry
*/
export interface RetryResult<F extends (...args: any[]) => any> extends Promise<ReturnType<F> | void> {
/**
* Cancels retrying.
*
* @param reason Optional reason to reject results with.
*/
cancel: (reason?: string) => void;
}
/**
* Retry the wrapped function according to the
*
* @param func the function to retry.
* @param delay a delay in milliseconds, an array of millisecond delays or
* [[RetryDelayCallback|callback]] to determine the delay before the next
* attempt.
*
* @param stop the number of attempts, or [[RetryStopCallback|callback]] to
* determine when retry should stop retrying `func`.
* @param args an array or [[RetryArgumentsCallback|callback]] to
* provide arguments to `func`
*
* @category Wrapper
*/
declare const retry: (func: (...args: any[]) => any, delay?: RetryDelay, stop?: RetryStop, args?: RetryArguments<(...args: any[]) => any>) => RetryResult<(...args: any[]) => any>;
export default retry;