async-wrappers
Version:
A set of wrapper functions to perform debouncing, throttling, retrying etc.
50 lines (49 loc) • 1.48 kB
TypeScript
/**
* A callback that will return a value after [[wait]] is completed.
*
* @typeparam T the type of the value.
*
* @category Wait
*/
declare type WaitValueCallback<T> =
/**
* @returns the value or promise to return the value
*/
() => T | PromiseLike<T>;
/**
* A promise will resolve with a value when [[wait]] is complete.
*
* @typeparam T the type of the value.
*
* @category Wait
*/
export interface WaitResult<T> extends Promise<T> {
/**
* Causes the promise to reject.
* @param error If suplied this will be the error the promise rejects with.
*/
cancel: (error?: Error) => void;
/**
* Stops waiting
* this will cause any waiting values to be resolved on the next runtime loop.
*/
stop: () => void;
}
/**
* Resolves after a given delay, optionally with a value.
*
* If the delay is `0` the promises will resolve after the current
* runtime event loop.
*
* @param delay The time in milliseconds before the value is returned.
* @param func A function or value that will be returned after waiting.
*
* @returns A promise that resolves with the value/function result.
* The promise has 2 extra functions defined:
* - `cancel` cancels the result and rejects the promise.
* - `stop` stops waiting and resolves the promised value.
*
* @category Wrapper
*/
declare const wait: <T = void>(delay: number, func?: T | PromiseLike<T> | WaitValueCallback<T> | undefined) => WaitResult<T>;
export default wait;