@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
61 lines (60 loc) • 3.55 kB
TypeScript
/**
* Lock all concurrent calls to a resource to one promise for a set duration of time.
* @param {string} key - Unique key to identify the promise.
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
* @param {number} duration - Duration in milliseconds to hold on to the promise result. (default=1000)
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
* @example
* // returns Promise<string>
* var initTests = await promiseLock<string>("initTests", async () => { ... }, 2);
*/
export declare function promiseLock<T>(key: string, promiseFunc: () => Promise<T>, duration?: number): Promise<T>;
/**
* Ensures that a promise runs only once
* @param {string} key - Unique key to identify the promise.
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
* @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
* by the promise is valid and should be kepy in memory.
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
* @example
* // returns Promise<string>
* var initTests = await promiseOnce<string>("initTests", async () => { ... });
*/
export declare function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T>;
/**
* Runs all promises in sequential order.
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
* provided promise functions
*/
export declare function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]>;
/**
* Runs N promises in parallel.
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
* @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
* provided promise functions
*/
export declare function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel?: number): Promise<T[]>;
/**
* Provides an asnyc sleep function that allows you to delay async/wait calls.
* @param {number} [seconds] - Time to sleep in seconds.
*/
export declare function sleepAsync(seconds?: number): Promise<void>;
/**
* Provides the ability to retry an async function n times with a optional delay between calls
* @param {(...args) => Promise<T>} fn - Function to retry,
* @param {number} numberOfRetries - Number of times to retry.
* @param {number} [seconds] - Delay between retries in seconds (default=1).
*/
export declare function retryAsync<T>(fn: (...args: any[]) => Promise<T>, numberOfRetries: number, seconds?: number): Promise<T>;
/**
* Provides the ability to have a promise/async funciton be exceuted with a timeout
* @param {number} fn - Promise to execute.
* @param {number} timeout - Timeout length in milliseconds.
* @returns {Promise<T>} Returns promise result or the promise is rejected error with a message of 'Timeout'.
*/
export declare function promiseWithTimeout<T>(fn: Promise<T>, timeout: number): Promise<T>;
export declare function promiseWithAbort<T>(fn: Promise<T>, options: {
signal: AbortSignal;
}): Promise<T>;