/** * An async reduce function. Works like the * built-in Array.reduce function but handles * an async reducer function */ export declare const reduce: (array: readonly T[], asyncReducer: (acc: K, item: T, index: number) => Promise, initValue?: K) => Promise; /** * An async map function. Works like the * built-in Array.map function but handles * an async mapper function */ export declare const map: (array: readonly T[], asyncMapFunc: (item: T, index: number) => Promise) => Promise; /** * Useful when for script like things where cleanup * should be done on fail or sucess no matter. * * You can call defer many times to register many * defered functions that will all be called when * the function exits in any state. */ export declare const defer: (func: (register: (fn: (error?: any) => any, options?: { rethrow?: boolean; }) => void) => Promise) => Promise; /** * Support for the built-in AggregateError * is still new. Node < 15 doesn't have it * so patching here. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility */ export declare class AggregateError extends Error { errors: Error[]; constructor(errors?: Error[]); } /** * Executes many async functions in parallel. Returns the * results from all functions as an array. After all functions * have resolved, if any errors were thrown, they are rethrown * in an instance of AggregateError */ export declare const parallel: (limit: number, array: readonly T[], func: (item: T) => Promise) => Promise; type PromiseValues[]> = { [K in keyof T]: T[K] extends Promise ? U : never; }; /** * Functionally similar to Promise.all or Promise.allSettled. If any * errors are thrown, all errors are gathered and thrown in an * AggregateError. * * @example * const [user] = await all([ * api.users.create(...), * s3.buckets.create(...), * slack.customerSuccessChannel.sendMessage(...) * ]) */ export declare function all, ...Promise[]]>(promises: T): Promise>; export declare function all[]>(promises: T): Promise>; /** * Functionally similar to Promise.all or Promise.allSettled. If any * errors are thrown, all errors are gathered and thrown in an * AggregateError. * * @example * const { user } = await all({ * user: api.users.create(...), * bucket: s3.buckets.create(...), * message: slack.customerSuccessChannel.sendMessage(...) * }) */ export declare function all>>(promises: T): Promise<{ [K in keyof T]: Awaited; }>; /** * Retries the given function the specified number * of times. */ export declare const retry: (options: { times?: number; delay?: number | null; backoff?: (count: number) => number; }, func: (exit: (err: any) => void) => Promise) => Promise; /** * Async wait */ export declare const sleep: (milliseconds: number) => Promise; /** * A helper to try an async function without forking * the control flow. Returns an error first callback _like_ * array response as [Error, result] */ export declare const tryit: (func: (...args: Args) => Return) => (...args: Args) => Return extends Promise ? Promise<[Error, undefined] | [undefined, Awaited]> : [Error, undefined] | [undefined, Return]; /** * A helper to try an async function that returns undefined * if it fails. * * e.g. const result = await guard(fetchUsers)() ?? []; */ export declare const guard: any>(func: TFunction, shouldGuard?: (err: any) => boolean) => ReturnType extends Promise ? Promise> | undefined> : ReturnType | undefined; export {};