export declare type AsyncQueueOption = {
/**
* Maximum concurrency limit
*/
limit?: number;
};
/**
* Queues the asynchronous tasks then processes sequentially under the maximum restriction.
*
* @example
*
* ```ts
* const aqueue = new AsyncQueue(1)
* const seq: number[] = []
* const res = await Promise.all([
* aqueue.run(() => new Promise(then => delay(200).then(() => { then(200); seq.push(200) }))),
* aqueue.run(() => new Promise(then => delay(100).then(() => { then(100); seq.push(100) }))),
* aqueue.run(() => new Promise(then => delay(150).then(() => { then(150); seq.push(150) })))
* ])
* assert.deepStrictEqual(seq, [200, 100, 150])
* assert.deepStrictEqual(res, [200, 100, 150])
* ```
*/
export declare class AsyncQueue {
private limit;
private count;
private queue;
/**
* Returns the total number of tasks currently being executed and in the waiting queue.
*/
get size(): number;
/**
* Alias of `size`.
*/
get length(): number;
/**
* @param limit Maximum concurrency limit
*/
constructor(limit?: number);
/**
* @param option Asynchronous Queue options
*/
constructor(option?: AsyncQueueOption);
private process;
private afterProcess;
/**
* Runs a asynchronous task in the async queue.
*/
run(f: () => Promise): Promise;
}
export declare type RetryOption = {
/**
* The time to wait between retries, in milliseconds. The default is 0.
*/
interval?: number;
/**
* The number of attempts to make before giving up. The default is 3.
*/
times?: number;
};
declare type Fn = ((times: number) => Promise) | (() => A);
/**
* Attempts to get a successful response from task no more than times times before returning an error.
*
* @example
*
* ```ts
* const res = await retry(() => new Promise(r => r(1)), 3)
* assert.deepStrictEqual(res, 1)
*
* const res = await retry(() => 2), { times: 3, interval: 300 })
* assert.deepStrictEqual(res, 2)
* ```
*/
export declare function retry(fn: Fn, times?: number): Promise;
export declare function retry(fn: Fn, options: RetryOption): Promise;
export declare type Thenable = ((...args: Params) => Promise) | ((...args: Params) => Data);
export interface DeferredOptions {
/**
* Optional delay in milliseconds before executing the function.
*
* @default 0
*/
delay?: number;
onError?: (e: unknown) => void;
onSuccess?: (data: D) => void;
}
export interface DeferredReturn {
execute: (delay?: number, ...args: Params) => Promise;
}
/**
* Creates a deferred function that wraps a promise or a thenable function, allowing delayed execution and handling of success and error cases.
*
* @param promise The promise or thenable function to be executed.
* @param options Optional configuration for the deferred function.
* @returns An object with the execute method that can be called to execute the deferred function.
*
* @example
*
* ```ts
* deferred(() => 1).execute().then(r => expect(r).toBe(1))
* deferred(() => { throw 0 }).execute().catch(err => expect(err).toBe(0))
* ```
*/
export declare const deferred: (promise: Promise | Thenable, options?: DeferredOptions | undefined) => DeferredReturn;
export {};