@hyperlane-xyz/utils
Version:
General utilities and types for the Hyperlane network
65 lines • 2.8 kB
TypeScript
/**
* Return a promise that resolves in ms milliseconds.
* @param ms Time to wait
*/
export declare function sleep(ms: number): Promise<void>;
/**
* Wait up to a given amount of time, and throw an error if the promise does not resolve in time.
* @param promise The promise to timeout on.
* @param timeoutMs How long to wait for the promise in milliseconds.
* @param message The error message if a timeout occurs.
*/
export declare function timeout<T>(promise: Promise<T>, timeoutMs?: number, message?: string): Promise<T>;
/**
* Run a callback with a timeout.
* @param timeoutMs How long to wait for the promise in milliseconds.
* @param callback The callback to run.
* @returns callback return value
* @throws Error if the timeout is reached before the callback completes
*/
export declare function runWithTimeout<T>(timeoutMs: number, callback: () => Promise<T>): Promise<T>;
/**
* Executes a fetch request that fails after a timeout via an AbortController.
* @param resource resource to fetch (e.g URL)
* @param options fetch call options object
* @param timeout timeout MS (default 10_000)
* @returns fetch response
*/
export declare function fetchWithTimeout(resource: RequestInfo, options?: RequestInit, timeout?: number): Promise<Response>;
/**
* Retries an async function if it raises an exception,
* using exponential backoff.
* @param runner callback to run
* @param attempts max number of attempts
* @param baseRetryMs base delay between attempts
* @returns runner return value
*/
export declare function retryAsync<T>(runner: () => T, attempts?: number, baseRetryMs?: number): Promise<T>;
/**
* Run a callback with a timeout, and retry if the callback throws an error.
* @param runner callback to run
* @param delayMs base delay between attempts
* @param maxAttempts maximum number of attempts
* @returns runner return value
*/
export declare function pollAsync<T>(runner: () => Promise<T>, delayMs?: number, maxAttempts?: number | undefined): Promise<T>;
/**
* An enhanced Promise.race that returns
* objects with the promise itself and index
* instead of just the resolved value.
*/
export declare function raceWithContext<T>(promises: Array<Promise<T>>): Promise<{
resolved: T;
promise: Promise<T>;
index: number;
}>;
/**
* Map an async function over a list xs with a given concurrency level
* Forked from https://github.com/celo-org/developer-tooling/blob/0c61e7e02c741fe10ecd1d733a33692d324cdc82/packages/sdk/base/src/async.ts#L128
*
* @param concurrency number of `mapFn` concurrent executions
* @param xs list of value
* @param mapFn mapping function
*/
export declare function concurrentMap<A, B>(concurrency: number, xs: A[], mapFn: (val: A, idx: number) => Promise<B>): Promise<B[]>;
//# sourceMappingURL=async.d.ts.map