asyncbox
Version:
A collection of small async/await utilities
75 lines • 3.85 kB
TypeScript
import B from 'bluebird';
import type { LongSleepOptions, WaitForConditionOptions } from './types.js';
/**
* An async/await version of setTimeout
* @param ms - The number of milliseconds to wait
*/
export declare function sleep(ms: number): Promise<void>;
/**
* Sometimes `Promise.delay` or `setTimeout` are inaccurate for large wait
* times. To safely wait for these long times (e.g. in the 5+ minute range), you
* can use `longSleep`.
*
* You can also pass a `progressCb` option which is a callback function that
* receives an object with the properties `elapsedMs`, `timeLeft`, and
* `progress`. This will be called on every wait interval so you can do your
* wait logging or whatever.
* @param ms - The number of milliseconds to wait
* @param options - Options for controlling the long sleep behavior
*/
export declare function longSleep(ms: number, { thresholdMs, intervalMs, progressCb }?: LongSleepOptions): Promise<void>;
/**
* An async/await way of running a method until it doesn't throw an error
* @param times - The maximum number of times to retry the function
* @param fn - The async function to retry
* @param args - Arguments to pass to the function
*/
export declare function retry<T = any>(times: number, fn: (...args: any[]) => Promise<T>, ...args: any[]): Promise<T | null>;
/**
* You can also use `retryInterval` to add a sleep in between retries. This can
* be useful if you want to throttle how fast we retry.
* @param times - The maximum number of times to retry the function
* @param sleepMs - The number of milliseconds to wait between retries
* @param fn - The async function to retry
* @param args - Arguments to pass to the function
*/
export declare function retryInterval<T = any>(times: number, sleepMs: number, fn: (...args: any[]) => Promise<T>, ...args: any[]): Promise<T | null>;
export declare const parallel: typeof B.all;
/**
* Fire and forget async function execution
* @param fn - The function to execute asynchronously
* @param args - Arguments to pass to the function
*/
export declare function asyncify(fn: (...args: any[]) => any | Promise<any>, ...args: any[]): void;
/**
* Similar to `Array.prototype.map`; runs in serial or parallel
* @param coll - The collection to map over
* @param mapper - The function to apply to each element
* @param runInParallel - Whether to run operations in parallel (default: true)
*/
export declare function asyncmap<T, R>(coll: T[], mapper: (value: T) => R | Promise<R>, runInParallel?: boolean): Promise<R[]>;
/**
* Similar to `Array.prototype.filter`
* @param coll - The collection to filter
* @param filter - The function to test each element
* @param runInParallel - Whether to run operations in parallel (default: true)
*/
export declare function asyncfilter<T>(coll: T[], filter: (value: T) => boolean | Promise<boolean>, runInParallel?: boolean): Promise<T[]>;
/**
* Takes a condition (a function returning a boolean or boolean promise), and
* waits until the condition is true.
*
* Throws a `/Condition unmet/` error if the condition has not been satisfied
* within the allocated time, unless an error is provided in the options, as the
* `error` property, which is either thrown itself, or used as the message.
*
* The condition result is returned if it is not falsy. If the condition throws an
* error then this exception will be immediately passed through.
*
* The default options are: `{ waitMs: 5000, intervalMs: 500 }`
* @param condFn - The condition function to evaluate
* @param options - Options for controlling the wait behavior
*/
export declare function waitForCondition<T>(condFn: () => Promise<T> | T, options?: WaitForConditionOptions): Promise<T>;
export type { Progress, ProgressCallback, LongSleepOptions, WaitForConditionOptions, } from './types.js';
//# sourceMappingURL=asyncbox.d.ts.map