// Type definitions for Async 3.2 // Project: https://github.com/caolan/async, https://caolan.github.io/async // Definitions by: Boris Yankov // Arseniy Maximov // Joe Herman // Angus Fenying // Pascal Martin // Etienne Rossignon // Lifeng Zhu // Tümay Çeber // Andrew Pensinger // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 export as namespace async; export interface Dictionary { [key: string]: T; } export type IterableCollection = T[] | IterableIterator | Dictionary; export interface ErrorCallback { (err?: E | null): void; } export interface AsyncBooleanResultCallback { (err?: E | null, truthValue?: boolean): void; } export interface AsyncResultCallback { (err?: E | null, result?: T): void; } export interface AsyncResultArrayCallback { (err?: E | null, results?: Array): void; } export interface AsyncResultObjectCallback { (err: E | undefined, results: Dictionary): void; } export interface AsyncFunction { (callback: (err?: E | null, result?: T) => void): void; } export interface AsyncFunctionEx { (callback: (err?: E | null, ...results: T[]) => void): void; } export interface AsyncIterator { (item: T, callback: ErrorCallback): void; } export interface AsyncForEachOfIterator { (item: T, key: number|string, callback: ErrorCallback): void; } export interface AsyncResultIterator { (item: T, callback: AsyncResultCallback): void; } export interface AsyncResultIteratorPromise { (item: T): Promise; } export interface AsyncMemoIterator { (memo: R | undefined, item: T, callback: AsyncResultCallback): void; } export interface AsyncBooleanIterator { (item: T, callback: AsyncBooleanResultCallback): void; } export interface AsyncWorker { (task: T, callback: ErrorCallback): void; } export interface AsyncVoidFunction { (callback: ErrorCallback): void; } export type AsyncAutoTasks, E> = { [K in keyof R]: AsyncAutoTask }; export type AsyncAutoTask, E> = AsyncAutoTaskFunctionWithoutDependencies | Array>; export interface AsyncAutoTaskFunctionWithoutDependencies { (cb: AsyncResultCallback | ErrorCallback): void; } export interface AsyncAutoTaskFunction, E = Error> { (results: R, cb: AsyncResultCallback | ErrorCallback): void; } export interface DataContainer { data: T; priority: number; } export interface CallbackContainer { callback: Function; } export interface PriorityContainer { priority: number; } export interface QueueObject { /** * Returns the number of items waiting to be processed. */ length(): number; /** * Indicates whether or not any items have been pushed and processed by the queue. */ started: boolean; /** * Returns the number of items currently being processed. */ running(): number; /** * Returns an array of items currently being processed. */ workersList, CallbackContainer>(): TWorker[]; /** * Returns false if there are items waiting or being processed, or true if not. */ idle(): boolean; /** * An integer for determining how many worker functions should be run in parallel. * This property can be changed after a queue is created to alter the concurrency on-the-fly. */ concurrency: number; /** * An integer that specifies how many items are passed to the worker function at a time. * Only applies if this is a cargo object */ payload: number; /** * Add a new task to the queue. Calls `callback` once the worker has finished * processing the task. * * Instead of a single task, a tasks array can be submitted. * The respective callback is used for every task in the list. */ push(task: T | T[]): Promise; push(task: T | T[], callback: AsyncResultCallback): void; /** * Add a new task to the front of the queue */ unshift(task: T | T[]): Promise; unshift(task: T | T[], callback: AsyncResultCallback): void; /** * The same as `q.push`, except this returns a promise that rejects if an error occurs. * The `callback` arg is ignored */ pushAsync(task: T | T[]): Promise; /** * The same as `q.unshift`, except this returns a promise that rejects if an error occurs. * The `callback` arg is ignored */ unshiftAsync(task: T | T[]): Promise; /** * Remove items from the queue that match a test function. * The test function will be passed an object with a `data` property, * and a `priority` property, if this is a `priorityQueue` object. */ remove(filter: (node: DataContainer) => boolean): void; /** * A function that sets a callback that is called when the number of * running workers hits the `concurrency` limit, and further tasks will be * queued. * * If the callback is omitted, `q.saturated()` returns a promise * for the next occurrence. */ saturated(): Promise; saturated(handler: () => void): void; /** * A function that sets a callback that is called when the number * of running workers is less than the `concurrency` & `buffer` limits, * and further tasks will not be queued * * If the callback is omitted, `q.unsaturated()` returns a promise * for the next occurrence. */ unsaturated(): Promise; unsaturated(handler: () => void): void; /** * A minimum threshold buffer in order to say that the `queue` is `unsaturated`. */ buffer: number; /** * A function that sets a callback that is called when the last item from the `queue` * is given to a `worker`. * * If the callback is omitted, `q.empty()` returns a promise for the next occurrence. */ empty(): Promise; empty(handler: () => void): void; /** * A function that sets a callback that is called when the last item from * the `queue` has returned from the `worker`. * * If the callback is omitted, `q.drain()` returns a promise for the next occurrence. */ drain(): Promise; drain(handler: () => void): void; /** * A function that sets a callback that is called when a task errors. * * If the callback is omitted, `q.error()` returns a promise that rejects on the next error. */ error(): Promise; error(handler: (error: Error, task: T) => void): void; /** * A boolean for determining whether the queue is in a paused state. */ paused: boolean; /** * A function that pauses the processing of tasks until `q.resume()` is called. */ pause(): void; /** * A function that resumes the processing of queued tasks when the queue * is paused. */ resume(): void; /** * A function that removes the drain callback and empties remaining tasks * from the queue forcing it to go idle. No more tasks should be pushed to * the queue after calling this function. */ kill(): void; } /** * A priorityQueue object to manage the tasks. * * There are two differences between queue and priorityQueue objects: * - `push(task, priority, [callback])` — priority should be a number. If an array of tasks is given, all tasks will be assigned the same priority. * - The `unshift` method was removed. */ // FIXME: can not use Omit due to ts version restriction. Replace Pick with Omit, when ts 3.5+ will be allowed export interface AsyncPriorityQueue extends Pick, Exclude, 'push' | 'unshift'>> { push(task: T | T[], priority?: number): Promise; push(task: T | T[], priority: number, callback: AsyncResultCallback): void; } /** * @deprecated this is a type that left here for backward compatibility. * Please use QueueObject instead */ export type AsyncQueue = QueueObject; /** * @deprecated this is a type that left here for backward compatibility. * Please use QueueObject instead */ export type AsyncCargoQueue = QueueObject; // Collections export function each(arr: IterableCollection, iterator: AsyncIterator, callback: ErrorCallback): void; export function each(arr: IterableCollection, iterator: AsyncIterator): Promise; export const eachSeries: typeof each; export function eachLimit(arr: IterableCollection, limit: number, iterator: AsyncIterator, callback: ErrorCallback): void; export function eachLimit(arr: IterableCollection, limit: number, iterator: AsyncIterator): Promise; export const forEach: typeof each; export const forEachSeries: typeof each; export const forEachLimit: typeof eachLimit; export function forEachOf(obj: IterableCollection, iterator: AsyncForEachOfIterator, callback: ErrorCallback): void; export function forEachOf(obj: IterableCollection, iterator: AsyncForEachOfIterator): Promise; export const forEachOfSeries: typeof forEachOf; export function forEachOfLimit(obj: IterableCollection, limit: number, iterator: AsyncForEachOfIterator, callback: ErrorCallback): void; export function forEachOfLimit(obj: IterableCollection, limit: number, iterator: AsyncForEachOfIterator): Promise; export const eachOf: typeof forEachOf; export const eachOfSeries: typeof forEachOf; export const eachOfLimit: typeof forEachOfLimit; export function map( arr: T[] | IterableIterator | Dictionary, iterator: (AsyncResultIterator | AsyncResultIteratorPromise), callback: AsyncResultArrayCallback ): void; export function map( arr: T[] | IterableIterator | Dictionary, iterator: (AsyncResultIterator | AsyncResultIteratorPromise) ): Promise; export const mapSeries: typeof map; export function mapLimit( arr: IterableCollection, limit: number, iterator: (AsyncResultIterator | AsyncResultIteratorPromise), callback: AsyncResultArrayCallback ): void; export function mapLimit( arr: IterableCollection, limit: number, iterator: (AsyncResultIterator | AsyncResultIteratorPromise) ): Promise; export function mapValuesLimit( obj: Dictionary, limit: number, iteratee: (value: T, key: string, callback: AsyncResultCallback) => void, callback: AsyncResultObjectCallback ): void; export function mapValuesLimit( obj: Dictionary, limit: number, iteratee: (value: T, key: string, callback: AsyncResultCallback) => void ): Promise; export function mapValues(obj: Dictionary, iteratee: (value: T, key: string, callback: AsyncResultCallback) => void, callback: AsyncResultObjectCallback): void; export function mapValues(obj: Dictionary, iteratee: (value: T, key: string, callback: AsyncResultCallback) => void): Promise; export const mapValuesSeries: typeof mapValues; export function filter(arr: IterableCollection, iterator: AsyncBooleanIterator, callback: AsyncResultArrayCallback): void; export function filter(arr: IterableCollection, iterator: AsyncBooleanIterator): Promise; export const filterSeries: typeof filter; export function filterLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback: AsyncResultArrayCallback): void; export function filterLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator): Promise; export const select: typeof filter; export const selectSeries: typeof filter; export const selectLimit: typeof filterLimit; export const reject: typeof filter; export const rejectSeries: typeof filter; export const rejectLimit: typeof filterLimit; export function reduce(arr: T[] | IterableIterator, memo: R, iterator: AsyncMemoIterator, callback: AsyncResultCallback): void; export function reduce(arr: T[] | IterableIterator, memo: R, iterator: AsyncMemoIterator): Promise; export const inject: typeof reduce; export const foldl: typeof reduce; export const reduceRight: typeof reduce; export const foldr: typeof reduce; export function detect(arr: IterableCollection, iterator: AsyncBooleanIterator, callback: AsyncResultCallback): void; export function detect(arr: IterableCollection, iterator: AsyncBooleanIterator): Promise; export const detectSeries: typeof detect; export function detectLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback?: AsyncResultCallback): void; export const find: typeof detect; export const findSeries: typeof detect; export const findLimit: typeof detectLimit; export function sortBy(arr: T[] | IterableIterator, iterator: AsyncResultIterator, callback?: AsyncResultArrayCallback): void; export function some(arr: IterableCollection, iterator: AsyncBooleanIterator, callback?: AsyncBooleanResultCallback): void; export const someSeries: typeof some; export function someLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback?: AsyncBooleanResultCallback): void; export const any: typeof some; export const anySeries: typeof someSeries; export const anyLimit: typeof someLimit; export function every(arr: IterableCollection, iterator: AsyncBooleanIterator, callback?: AsyncBooleanResultCallback): void; export const everySeries: typeof every; export function everyLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback?: AsyncBooleanResultCallback): void; export const all: typeof every; export const allSeries: typeof every; export const allLimit: typeof everyLimit; export function concat(arr: IterableCollection, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; export function concat(arr: IterableCollection, iterator: AsyncResultIterator): Promise; export function concatLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; export function concatLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator): Promise; export const concatSeries: typeof concat; // Control Flow export function series(tasks: Array>, callback?: AsyncResultArrayCallback): void; export function series(tasks: Dictionary>, callback?: AsyncResultObjectCallback): void; export function series(tasks: Array> | Dictionary>): Promise; export function parallel(tasks: Array>, callback?: AsyncResultArrayCallback): void; export function parallel(tasks: Dictionary>, callback?: AsyncResultObjectCallback): void; export function parallel(tasks: Array> | Dictionary>): Promise; export function parallelLimit(tasks: Array>, limit: number, callback?: AsyncResultArrayCallback): void; export function parallelLimit(tasks: Dictionary>, limit: number, callback?: AsyncResultObjectCallback): void; export function parallelLimit(tasks: Array> | Dictionary>, limit: number): Promise; export function whilst( test: (cb: AsyncBooleanResultCallback) => void, fn: AsyncFunctionEx, callback: AsyncFunctionEx ): void; export function whilst( test: (cb: AsyncBooleanResultCallback) => void, fn: AsyncFunctionEx ): Promise; export function doWhilst( fn: AsyncFunctionEx, test: (/* ...results: T[], */ cb: AsyncBooleanResultCallback) => void, callback: AsyncFunctionEx ): void; export function doWhilst( fn: AsyncFunctionEx, test: (/* ...results: T[], */ cb: AsyncBooleanResultCallback) => void ): Promise; export function until( test: (cb: AsyncBooleanResultCallback) => void, fn: AsyncFunctionEx, callback: AsyncFunctionEx ): void; export function until( test: (cb: AsyncBooleanResultCallback) => void, fn: AsyncFunctionEx ): Promise; export function doUntil( fn: AsyncFunctionEx, test: (/* ...results: T[], */ cb: AsyncBooleanResultCallback) => void, callback: AsyncFunctionEx ): void; export function doUntil( fn: AsyncFunctionEx, test: (/* ...results: T[], */ cb: AsyncBooleanResultCallback) => void ): Promise; export function during(test: (testCallback: AsyncBooleanResultCallback) => void, fn: AsyncVoidFunction, callback: ErrorCallback): void; export function doDuring(fn: AsyncVoidFunction, test: (testCallback: AsyncBooleanResultCallback) => void, callback: ErrorCallback): void; export function forever(next: (next: ErrorCallback) => void, errBack: ErrorCallback): void; export function waterfall(tasks: Function[], callback?: AsyncResultCallback): void; export function compose(...fns: Function[]): Function; export function seq(...fns: Function[]): Function; export function applyEach(fns: Function[], ...argsAndCallback: any[]): void; // applyEach(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. export function applyEachSeries(fns: Function[], ...argsAndCallback: any[]): void; // applyEachSeries(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. export function queue(worker: AsyncWorker, concurrency?: number): QueueObject; export function queue(worker: AsyncResultIterator, concurrency?: number): QueueObject; export function priorityQueue(worker: AsyncWorker, concurrency?: number): AsyncPriorityQueue; export function cargo(worker: AsyncWorker, payload?: number): QueueObject; export function cargoQueue( worker: AsyncWorker, concurrency?: number, payload?: number, ): QueueObject; export function auto, E = Error>(tasks: AsyncAutoTasks, concurrency?: number): Promise; export function auto, E = Error>(tasks: AsyncAutoTasks, concurrency: number, callback: AsyncResultCallback): void; export function auto, E = Error>(tasks: AsyncAutoTasks, callback: AsyncResultCallback): void; export function autoInject(tasks: any, callback?: AsyncResultCallback): void; export interface RetryOptions { times?: number | undefined; interval?: number | ((retryCount: number) => number) | undefined; errorFilter?: ((error: E) => boolean) | undefined; } export function retry( task: (() => Promise) | ((callback: AsyncResultCallback) => void), ): Promise; export function retry( opts: number | RetryOptions, task: (() => Promise) | ((callback: AsyncResultCallback) => void), ): Promise; export function retry( task: (() => Promise) | ((callback: AsyncResultCallback) => void), callback: AsyncResultCallback, ): void; export function retry( opts: number | RetryOptions, task: (() => Promise) | ((callback: AsyncResultCallback) => void), callback: AsyncResultCallback, ): void; export function retryable(task: AsyncFunction): AsyncFunction; export function retryable( opts: number | (RetryOptions & { arity?: number | undefined }), task: AsyncFunction, ): AsyncFunction; export function apply(fn: Function, ...args: any[]): AsyncFunction; export function nextTick(callback: Function, ...args: any[]): void; export const setImmediate: typeof nextTick; export function reflect(fn: AsyncFunction): (callback: (err: null, result: {error?: E | undefined, value?: T | undefined}) => void) => void; export function reflectAll(tasks: Array>): Array<(callback: (err: null, result: {error?: E | undefined, value?: T | undefined}) => void) => void>; export function timeout(fn: AsyncFunction, milliseconds: number, info?: any): AsyncFunction; export function timeout(fn: AsyncResultIterator, milliseconds: number, info?: any): AsyncResultIterator; export function times(n: number, iterator: (AsyncResultIterator | AsyncResultIteratorPromise), callback: AsyncResultArrayCallback): void; export function times(n: number, iterator: (AsyncResultIterator | AsyncResultIteratorPromise)): Promise; export const timesSeries: typeof times; export function timesLimit( n: number, limit: number, iterator: (AsyncResultIterator | AsyncResultIteratorPromise), callback: AsyncResultArrayCallback ): void; export function timesLimit( n: number, limit: number, iterator: (AsyncResultIterator | AsyncResultIteratorPromise) ): Promise; export function transform(arr: T[], iteratee: (acc: R[], item: T, key: number, callback: (error?: E) => void) => void, callback?: AsyncResultArrayCallback): void; export function transform(arr: T[], acc: R[], iteratee: (acc: R[], item: T, key: number, callback: (error?: E) => void) => void, callback?: AsyncResultArrayCallback): void; export function transform( arr: {[key: string]: T}, iteratee: (acc: {[key: string]: R}, item: T, key: string, callback: (error?: E) => void) => void, callback?: AsyncResultObjectCallback ): void; export function transform( arr: {[key: string]: T}, acc: {[key: string]: R}, iteratee: (acc: {[key: string]: R}, item: T, key: string, callback: (error?: E) => void) => void, callback?: AsyncResultObjectCallback ): void; export function race(tasks: Array>, callback: AsyncResultCallback): void; // Utils export function memoize(fn: Function, hasher?: Function): Function; export function unmemoize(fn: Function): Function; export function ensureAsync(fn: (... argsAndCallback: any[]) => void): Function; export function constant(...values: any[]): AsyncFunction; export function asyncify(fn: Function): (...args: any[]) => any; export function wrapSync(fn: Function): Function; export function log(fn: Function, ...args: any[]): void; export function dir(fn: Function, ...args: any[]): void;