villa
Version:
Promise utilities for async/await-ready environment.
46 lines (45 loc) • 1.64 kB
TypeScript
import { Resolvable } from '.';
/**
* A no-operation function that acts as the rejection handler of a promise.
*/
export declare function bear(_error: any): undefined;
/**
* Create a promise that will be fulfilled in given duration (milliseconds).
* @param duration Duration in milliseconds before the promise gets fulfilled.
*/
export declare function sleep(duration: number): Promise<void>;
export declare type RetryHandler<TResult> = (lastReason: any, attempts: number) => Resolvable<TResult>;
export interface RetryOptions {
/** Try limit times (defaults to 3). */
limit?: number;
/** Interval between two tries (defaults to 0). */
interval?: number;
}
/**
* Retry procedure in the handler for several times.
* @param handler Retry handler.
* @param options Retry options.
* @return Created promise.
*/
export declare function retry<T>(handler: RetryHandler<T>, options?: RetryOptions): Promise<T>;
export declare type BatchSchedulerHandler<T> = (tasks: T[]) => Resolvable<void>;
/**
* BatchScheduler provides the ability to handle tasks scheduled within a time
* span in batch.
*/
export declare class BatchScheduler<T> {
private handler;
private timeBeforeStart;
private tasks;
private batchPromise;
/**
* Construct a BatchScheduler instance.
*
* @param handler A batch handler for the tasks put together.
* @param timeBeforeStart The time span within which tasks would be handled
* in batch.
*/
constructor(handler: BatchSchedulerHandler<T>, timeBeforeStart?: number | undefined);
/** Schedule a task. */
schedule(task: T): Promise<void>;
}