sussy-util
Version:
Util package made by me
150 lines (149 loc) • 7.02 kB
TypeScript
import { PromiseOr, AnyFunction } from '../Types';
export declare type resolver = (value: unknown) => unknown;
export declare type ErrorHandlingStrategy = 'first' | 'replace' | 'last';
export declare type AsyncFunction<R> = () => PromiseOr<R>;
export declare type CallbackPosition = 'front' | 'back';
export type ResolverFn = (value?: unknown) => void;
export type MappingKey = `${CallbackPosition}-${ErrorHandlingStrategy}`;
export type ResFirst = <R, E extends Error>(resolve: ResolverFn, reject: ResolverFn, result: R, error: E | null) => void;
export type ResReplace = <R, E extends Error>(resolve: ResolverFn, reject: ResolverFn, result: R | E) => void;
export type ResLast = <R, E extends Error>(resolve: ResolverFn, reject: ResolverFn, error: E | null, result: R) => void;
export type MappingTable = {
'front-first': {
args: (args: unknown[], callback: AnyFunction) => unknown[];
function1: ResLast;
};
'front-replace': {
args: (args: unknown[], callback: AnyFunction) => unknown[];
function1: ResReplace;
};
'front-last': {
args: (args: unknown[], callback: AnyFunction) => unknown[];
function1: ResFirst;
};
'back-first': {
args: (args: unknown[], callback: AnyFunction) => unknown[];
function1: ResLast;
};
'back-replace': {
args: (args: unknown[], callback: AnyFunction) => unknown[];
function1: ResReplace;
};
'back-last': {
args: (args: unknown[], callback: AnyFunction) => unknown[];
function1: ResFirst;
};
};
export interface PromisifyOptions {
callBackPosition?: CallbackPosition;
errorPosition?: ErrorHandlingStrategy;
}
declare class PromiseUtil {
#private;
/**
* Singleton instance of the PromiseUtil class.
* @private
* @static
*/
private static readonly instance;
private constructor();
/**
* Execute an array of promises sequentially and collect results and errors.
*
* @template T - Type of the promise results.
* @param {PromiseOr<T>[]} promises - Array of promises to execute.
* @returns {Promise<[Array<T | null>, Error[]]>} - A promise that resolves to an array of results and errors.
*/
executeSequentially<T>(promises: PromiseOr<T>[]): Promise<[Array<T | null>, Error[]]>;
/**
* Handle a promise, including error handling.
*
* @template R - Type of the promise result.
* @template E - Type of the error.
* @param {PromiseOr<R> | AsyncFunction<R>} promise - The promise to handle.
* @returns {Promise<[R | null, E | null]>} - A promise that resolves to a tuple of result and error.
*/
handler<R, E extends Error>(promise: PromiseOr<R> | AsyncFunction<R>): Promise<[R | null, E | null]>;
/**
* Retry executing a function with a specified number of retries.
*
* @template T - Type of the function result.
* @param {() => PromiseOr<T>} func - The function to retry.
* @param {number} [maxRetries=3] - The maximum number of retries.
* @returns {Promise} A promise that resolves to the result and an array of errors.
*/
retry<T>(func: () => PromiseOr<T>, maxRetries?: number): Promise<[T | null, Error[]]>;
/**
* Delay execution for a specified number of milliseconds.
*
* @param {number} milliseconds - The delay duration in milliseconds.
* @returns {Promise<void>} A promise that resolves after the delay.
*/
delay(milliseconds: number): Promise<void>;
/**
* Filter an array of values using an asynchronous filter function.
*
* @template T - Type of the values.
* @param {T[]} values - The array of values to filter.
* @param {(value: T) => PromiseOr<boolean>} asyncFilterFunc - The asynchronous filter function.
* @returns {Promise<T[]>} A promise that resolves to the filtered array.
*/
filter<T>(values: T[], asyncFilterFunc: (value: T) => PromiseOr<boolean>): Promise<T[]>;
/**
* Timeout a promise after a specified duration.
*
* @template T - Type of the promise result.
* @param {PromiseOr<T>} promise - The promise to add a timeout to.
* @param {number} milliseconds - The timeout duration in milliseconds.
* @returns {Promise<T>} A promise that resolves with the result or rejects with a timeout error.
*/
timeout<T>(promise: PromiseOr<T>, milliseconds: number): Promise<T>;
/**
* Execute promises in batches and collect results.
*
* @template T - Type of input values.
* @template R - Type of promise results.
* @param {T[]} values - Array of input values.
* @param {number} batchSize - Size of each batch.
* @param {(value: T) => PromiseOr<R>} asyncFunc - Asynchronous function to apply to each value.
* @returns {Promise<R[]>} - A promise that resolves to an array of results.
*/
batchPromises<T, R>(values: T[], batchSize: number, asyncFunc: (value: T) => PromiseOr<R>): Promise<R[]>;
/**
* Promisify a function, allowing customization of callback and error positions.
*
* @template T - Type of arguments to the function.
* @template R - Type of the promise result.
* @param {Function} func - The function to promisify.
* @param {options} [options={ callBackPosition: "back", errorPosition: "last" }] - Options for customization.
* @returns {Function} A promisified function.
*/
promisify<T extends unknown[], R>(func: AnyFunction, { callBackPosition, errorPosition }?: PromisifyOptions): (...args: T) => Promise<R>;
/**
* Map an array of values to an array of promises and await their results.
* @param values Array of values to map to promises.
* @param asyncMapFunc Function to map values to promises.
* @returns Array of results.
*/
mapPromises<T, R>(values: T[], asyncMapFunc: (value: T) => PromiseOr<R>): Promise<R[]>;
/**
* Concurrently map an array of values to promises with a specified concurrency limit.
* @param values Array of values to map to promises.
* @param asyncMapFunc Function to map values to promises.
* @param concurrency Maximum number of promises to run concurrently.
* @returns Array of results.
*/
concurrentMap<T, R>(values: T[], asyncMapFunc: (value: T) => PromiseOr<R>, concurrency: number): Promise<R[]>;
/**
* Execute an array of promises concurrently with a specified concurrency limit.
*
* @template T - Type of the promise results.
* @param {PromiseOr<T>[]} promises - Array of promises to execute.
* @param {number} concurrency - Maximum number of promises to run concurrently.
* @returns {Promise<[Array<T | null>, Error[]]>} - A promise that resolves to an array of results and errors.
*/
executeConcurrently<T>(promises: PromiseOr<T>[], concurrency: number): Promise<[Array<T | null>, Error[]]>;
static getInstance(): PromiseUtil;
}
declare const _default: PromiseUtil;
export default _default;