/** * A function decorated with a `.cancel()` method that aborts pending invocations. * * @group Async */ export type Cancellable unknown> = F & { /** * Cancels any pending invocation. Calls already in flight are unaffected. */ cancel(): void; }; /** * Returns a promise that resolves after the specified number of milliseconds. * * @group Async * @example * ``` * await delay(1000) // wait for 1 second * ``` */ export declare function delay( /** * The number of milliseconds to delay. */ timeInMs: number): Promise; /** * Creates and returns a new debounced version of the passed function that will postpone its execution * until after wait milliseconds have elapsed since the last time it was invoked. * * The returned function exposes `.cancel()` to discard a pending trailing call. * * @group Async * @example * ``` * const debounced = debounce(() => console.log('debounced'), 100) * debounced() // will be called after 100ms * debounced.cancel() // discard the pending call * ``` */ export declare function debounce( /** * The function to be debounced. */ func: (...args: A) => void, /** * The number of milliseconds to delay. */ timeInMs: number): Cancellable<(this: unknown, ...args: A) => void>; /** * Creates a throttled function that will execute `func` at most once per `timeInMs` interval. * * The returned function exposes `.cancel()` to discard the pending trailing call. * * @group Async * @example * ``` * const throttled = throttle(() => console.log('throttled'), 100) * throttled() // will be called immediately * throttled() // will be ignored * await delay(100) * throttled() // will be called after 100ms * ``` */ export declare function throttle( /** * The function to be throttled. */ func: (...args: A) => void, /** * The number of milliseconds to throttle invocations to. */ timeInMs: number, /** * Options object. */ options?: { /** * If `true` the `func` will be executed on the leading edge of the timeout. * @defaultValue `true` */ leading?: boolean; /** * If `true` the `func` will be executed on the trailing edge of the timeout. * @defaultValue `true` */ trailing?: boolean; }): Cancellable<(this: unknown, ...args: A) => void>; /** * Error thrown by [[timeout]] when the deadline is reached. * * @group Async */ export declare class TimeoutError extends Error { constructor(message?: string); } /** * Rejects with a [[TimeoutError]] if `promise` doesn't settle within `ms` milliseconds. * * The original promise still runs to completion — there's no cancellation primitive * in JS — but the returned promise won't wait for it. * * @group Async * @example * ``` * await timeout(fetch('/slow'), 5000) * ``` */ export declare function timeout(promise: Promise, ms: number): Promise; /** * Retries an async function up to `attempts` times. * * `backoff` controls the delay between attempts in milliseconds — either a fixed number * or a function `(attemptNumber) => delayMs` (1-indexed). Defaults to no delay. * * The error from the last failed attempt is rethrown. * * @group Async * @example * ``` * await retry(() => fetch('/flaky'), { attempts: 3, backoff: 200 }) * await retry(fetchUser, { attempts: 5, backoff: n => 100 * 2 ** (n - 1) }) // exponential * ``` */ export declare function retry(fn: () => Promise, options?: { /** Total number of attempts (≥ 1). Default `3`. */ attempts?: number; /** Delay between attempts in ms, or a function of attempt number (1-indexed). */ backoff?: number | ((attempt: number) => number); }): Promise; /** * Returns a wrapper that runs at most `concurrency` async tasks at a time. * * Tasks queue up beyond the limit. The wrapper returns the same value/rejection * the underlying task does, so it composes naturally with `Promise.all`. * * @group Async * @example * ``` * const limit = pLimit(2) * await Promise.all(urls.map(u => limit(() => fetch(u)))) * ``` */ export declare function pLimit(concurrency: number): (fn: () => Promise) => Promise; /** * Runs the given task functions sequentially and collects their results. * * Stops at the first rejection. If you want to keep going on errors, wrap each * task in a `tryCatch` (from `fp.ts`) before passing it. * * Return type preserves the tuple shape of the input so heterogeneous return * types are inferred individually instead of being unified. * * @group Async * @example * ``` * const [user, post] = await pSeries([ * () => loadUser(1), // () => Promise * () => loadPost(2), // () => Promise * ]) * // user: User, post: Post * ``` */ export declare function pSeries Promise>>(tasks: T): Promise<{ -readonly [K in keyof T]: T[K] extends () => Promise ? R : never; }>;