@storm-stack/utilities
Version:
This package includes various base utility class and various functions to assist in the development process.
73 lines (72 loc) • 2.01 kB
TypeScript
interface DelayOptions {
signal?: AbortSignal;
}
/**
* Delays the execution of code for a specified number of milliseconds.
*
* This function returns a Promise that resolves after the specified delay, allowing you to use it
* with async/await to pause execution.
*
* @example
* ```typescript
* async function foo() {
* console.log('Start');
* await delay(1000); // Delays execution for 1 second
* console.log('End');
* }
*
* foo();
*
* // With AbortSignal
* const controller = new AbortController();
* const { signal } = controller;
*
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
* try {
* await delay(100, { signal });
* } catch (error) {
* console.error(error); // Will log 'AbortError'
* }
* }
* ```
*
* @param ms - The number of milliseconds to delay.
* @param options - The options object.
* @returns A Promise that resolves after the specified delay.
*/
export declare function delay(ms: number, { signal }?: DelayOptions): Promise<void>;
/**
* Delays the execution of code for a specified number of milliseconds.
*
* This function returns a Promise that resolves after the specified delay, allowing you to use it
* with async/await to pause execution.
*
* @example
* ```typescript
* async function foo() {
* console.log('Start');
* await sleep(1000); // Delays execution for 1 second
* console.log('End');
* }
*
* foo();
*
* // With AbortSignal
* const controller = new AbortController();
* const { signal } = controller;
*
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
* try {
* await sleep(100, { signal });
* } catch (error) {
* console.error(error); // Will log 'AbortError'
* }
* }
* ```
*
* @param ms - The number of milliseconds to sleep.
* @param options - The options object.
* @returns A Promise that resolves after the specified sleep.
*/
export declare function sleep(ms: number, options?: DelayOptions): Promise<void>;
export {};