UNPKG

@altostra/core

Version:

Core library for shared types and logic

80 lines (79 loc) 2.84 kB
import type { NaturalNumber } from "../../CustomTypes/Numerics"; import type { Maybe } from "../../Maybe"; /** * Polling options */ export interface PollWaitOptions { /** * Specify the polling interval */ intervalMillisecs: number; /** * Specify how many polls attempts are performed, before the operation fails */ maxTrials: number; /** * Specify a global timeout, after which the operation fails */ timeoutMillisecs: number; } /** * Polls for some data, until the polled data satisfy a specified predicate * @param poll The function to poll the data * @param statePredicate A predicate to check if polling is completed * @param options Optional polling options * @returns The polled data that was satisfied the provided predicate */ export declare function pollWait<TState>(poll: () => Promise<TState>, statePredicate: (state: TState) => boolean, options?: Partial<PollWaitOptions>): Promise<TState>; /** * Run all actions with limited concurrency * @param concurrency Max number of concurrent workers * @param actions An iterable of functions that perform async operations * * @return Promise for array of results from all actions. */ export declare function runWithLimit<T>(concurrency: NaturalNumber, actions: Iterable<() => Promise<T>>): Promise<T[]>; /** * Options for `whenAll` * @see whenAll */ export interface WhenAllOptions<T> { /** * An iterable of promises to wait for fulfillment */ promises: Iterable<Promise<T>>; /** * A function that executed if any promise was rejected with the index and rejection reason */ errorsHandler: (errors: WhenAllError[]) => Promise<void> | void; } /** * Return a promise that is fulfilled when all the provided promises are either fulfilled * or rejected, with the fulfilled results * @param options @see WhenAllOptions * @returns A promise for an array of the results of all the promises that were fulfilled */ export declare function whenAll<T>({ promises, errorsHandler, }: WhenAllOptions<T>): Promise<Maybe<T>[]>; /** * A promise rejection reason and the rejected promise index */ export interface WhenAllError { /** * The promise rejection reason (thrown error) */ error: any; /** * The index of the promise that was rejected */ index: number; } /** * Returns a promise that is resolved after the specified duration in milliseconds * @param milliseconds The delay after which the promise gets resolved */ export declare function delay(milliseconds: NaturalNumber | 0): Promise<void>; /** * Returns a promise that is rejected after the specified duration in milliseconds * @param milliseconds The delay after which the promise gets resolved */ export declare function timeout(milliseconds: NaturalNumber | 0, errorMessage: string): Promise<never>;