diff --git a/types/async/index.d.ts b/types/async/index.d.ts index ebbcb495b76189..bf6144e40d9051 100644 --- a/types/async/index.d.ts +++ b/types/async/index.d.ts @@ -8,26 +8,28 @@ // Dmitri Trofimov // Etienne Rossignon // Lifeng Zhu +// Simon Chan // 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 type IterableCollection = T[] | IterableIterator | AsyncIterableIterator | 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 AsyncResultExCallback { (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 AsyncForEachOfIterator { (item: T, key: number | string, callback: ErrorCallback): void; } export interface AsyncResultIterator { (item: T, callback: AsyncResultCallback): void; } -export interface AsyncMemoIterator { (memo: R | undefined, item: T, callback: AsyncResultCallback): void; } +export interface AsyncMemoIterator { (memo: R, item: T, callback: AsyncResultCallback): void; } export interface AsyncBooleanIterator { (item: T, callback: AsyncBooleanResultCallback): void; } export interface AsyncWorker { (task: T, callback: ErrorCallback): void; } @@ -56,7 +58,11 @@ export interface AsyncQueue { running(): number; idle(): boolean; concurrency: number; - push(task: T | T[], callback?: AsyncResultCallback): void; + + push(task: T | T[], callback: AsyncResultCallback): void; + push(task: T): Promise; + push(task: T[]): Array>; + unshift(task: T | T[], callback?: ErrorCallback): void; remove(filter: (node: DataContainer) => boolean): void; @@ -76,7 +82,9 @@ export interface AsyncQueue { error(): Promise; error(handler: (error: Error, task: T) => void): void; - unsaturated(): void; + unsaturated(handler: () => void): void; + unsaturated(): Promise; + buffer: number; } @@ -114,143 +122,218 @@ export interface AsyncCargo { } // Collections -export function each(arr: IterableCollection, iterator: AsyncIterator, callback?: ErrorCallback): void; +export function each(arr: IterableCollection, iterator: AsyncIterator, callback: ErrorCallback): void; +export function each(arr: T[], iterator: (item: T, callback: ErrorCallback) => void): 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, 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, 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, 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, callback?: AsyncResultArrayCallback): void; +export function map(arr: IterableCollection, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; +export function map(arr: IterableCollection, iterator: AsyncResultIterator): Promise; export const mapSeries: typeof map; -export function mapLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator, callback?: AsyncResultArrayCallback): void; +export function mapLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; +export function mapLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator): Promise; export function mapValuesLimit( obj: Dictionary, limit: number, iteratee: (value: T, key: string, callback: AsyncResultCallback) => void, callback: AsyncResultObjectCallback - ): void; +): 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, 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, callback: AsyncResultArrayCallback): void; +export function filterLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator): Promise; +export function groupBy(arr: IterableCollection, iterator: AsyncResultIterator, callback: AsyncResultObjectCallback): void; +export function groupBy(arr: IterableCollection, iterator: AsyncResultIterator): Promise>; +export function groupByLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator, callback: AsyncResultObjectCallback): void; +export function groupByLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator): Promise>; +export const groupBySeries: typeof groupBy; 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: IterableCollection, memo: R, iterator: AsyncMemoIterator, callback: AsyncResultCallback): void; +export function reduce(arr: IterableCollection, 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, 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 function detectLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback: AsyncResultCallback): void; +export function detectLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator): Promise; 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 function sortBy(arr: IterableCollection, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; +export function sortBy(arr: IterableCollection, iterator: AsyncResultIterator): Promise; +export function some(arr: IterableCollection, iterator: AsyncBooleanIterator, callback: AsyncBooleanResultCallback): void; +export function some(arr: IterableCollection, iterator: AsyncBooleanIterator): Promise; export const someSeries: typeof some; -export function someLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback?: AsyncBooleanResultCallback): void; +export function someLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback: AsyncBooleanResultCallback): void; +export function someLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator): Promise; 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 function every(arr: IterableCollection, iterator: AsyncBooleanIterator, callback: AsyncBooleanResultCallback): void; +export function every(arr: IterableCollection, iterator: AsyncBooleanIterator): Promise; export const everySeries: typeof every; -export function everyLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback?: AsyncBooleanResultCallback): void; +export function everyLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator, callback: AsyncBooleanResultCallback): void; +export function everyLimit(arr: IterableCollection, limit: number, iterator: AsyncBooleanIterator): Promise; 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 concatLimit(arr: IterableCollection, limit: number, iterator: AsyncResultIterator, callback?: AsyncResultArrayCallback): void; +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 parallel(tasks: Array>, callback?: AsyncResultArrayCallback): void; -export function parallel(tasks: Dictionary>, callback?: AsyncResultObjectCallback): void; -export function parallelLimit(tasks: Array>, limit: number, callback?: AsyncResultArrayCallback): void; -export function parallelLimit(tasks: Dictionary>, limit: number, callback?: AsyncResultObjectCallback): void; -export function whilst(test: () => boolean, fn: AsyncVoidFunction, callback: ErrorCallback): void; -export function doWhilst(fn: AsyncFunctionEx, test: (...results: T[]) => boolean, callback: ErrorCallback): void; -export function until(test: () => boolean, fn: AsyncVoidFunction, callback: ErrorCallback): void; -export function doUntil(fn: AsyncFunctionEx, test: (...results: T[]) => boolean, callback: ErrorCallback): void; -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 series(tasks: Array>, callback: AsyncResultArrayCallback): void; +export function series(tasks: Array>): Promise; +export function series(tasks: Dictionary>, callback: AsyncResultObjectCallback): void; +export function series(tasks: Dictionary>): Promise>; +export function parallel(tasks: Array>, callback: AsyncResultArrayCallback): void; +export function parallel(tasks: Array>): Promise; +export function parallel(tasks: Dictionary>, callback: AsyncResultObjectCallback): void; +export function parallel(tasks: Dictionary>): Promise>; +export function parallelLimit(tasks: Array>, limit: number, callback: AsyncResultArrayCallback): void; +export function parallelLimit(tasks: Array>, limit: number): Promise; +export function parallelLimit(tasks: Dictionary>, limit: number, callback: AsyncResultObjectCallback): void; +export function parallelLimit(tasks: Dictionary>, limit: number): Promise>; +export function whilst(test: AsyncFunction, fn: AsyncFunctionEx, callback: AsyncResultExCallback): void; +export function whilst(test: AsyncFunction, fn: AsyncFunctionEx): Promise; +export function doWhilst(fn: AsyncFunctionEx, test: Function, callback: AsyncResultExCallback): void; +export function doWhilst(fn: AsyncFunctionEx, test: Function): Promise; +export const until: typeof whilst; +export const doUntil: typeof doWhilst; +export const during: typeof whilst; +export const doDuring: typeof doWhilst; export function forever(next: (next: ErrorCallback) => void, errBack: ErrorCallback): void; -export function waterfall(tasks: Function[], callback?: AsyncResultCallback): void; +export function forever(next: (next: ErrorCallback) => void): Promise; +export function waterfall(tasks: Function[], callback: AsyncResultCallback): void; +export function waterfall(tasks: Function[]): Promise; 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. +// applyEach(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. +export function applyEach(fns: Function[], ...argsAndCallback: any[]): Function; +// applyEachSeries(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. +export function applyEachSeries(fns: Function[], ...argsAndCallback: any[]): Function; export function queue(worker: AsyncWorker, concurrency?: number): AsyncQueue; export function queue(worker: AsyncResultIterator, concurrency?: number): AsyncQueue; export function priorityQueue(worker: AsyncWorker, concurrency: number): AsyncPriorityQueue; export function cargo(worker: (tasks: any[], callback: ErrorCallback) => void, payload?: number): AsyncCargo; -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 function auto, E = Error>(tasks: AsyncAutoTasks, concurrency: number, callback: AsyncResultCallback): void; +export function auto, E = Error>(tasks: AsyncAutoTasks, concurrency?: number): Promise; +export function auto, E = Error>(tasks: AsyncAutoTasks, callback: AsyncResultCallback): void; +export function autoInject(tasks: any, callback: AsyncResultCallback): void; +export function autoInject(tasks: any): Promise; export function retry( opts: number | { - times: number, - interval: number | ((retryCount: number) => number), + times?: number, + interval?: number | ((retryCount: number) => number), + errorFilter?: (error: Error) => boolean + }, + task: AsyncFunctionEx, + callback: AsyncResultExCallback +): void; +export function retry( + task: AsyncFunctionEx, + callback: AsyncResultExCallback +): void; +export function retry( + opts: number | { + times?: number, + interval?: number | ((retryCount: number) => number), errorFilter?: (error: Error) => boolean }, - task: (callback: AsyncResultCallback, results: any) => void, - callback: AsyncResultCallback - ): void; + task: AsyncFunctionEx, +): Promise; +export function retry( + task: AsyncFunctionEx, +): Promise; -export function retryable(opts: number | {times: number, interval: number}, task: AsyncFunction): AsyncFunction; +export function retryable(opts: number | { times?: number, interval?: number }, task: Function): Function; +export function retryable(task: Function): Function; 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, value?: T}) => void) => void; -export function reflectAll(tasks: Array>): Array<(callback: (err: null, result: {error?: E, value?: T}) => void) => void>; +export function reflect(fn: AsyncFunction): (callback: (err: null, result: { error?: E, value?: T }) => void) => void; +export function reflectAll(tasks: Array>): Array<(callback: (err: null, result: { error?: E, value?: T }) => 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, callback: AsyncResultArrayCallback): void; -export function timesSeries(n: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; +export function times(n: number, iterator: AsyncResultIterator): Promise; export function timesLimit(n: number, limit: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; +export function timesLimit(n: number, limit: number, iterator: AsyncResultIterator): Promise; +export const timesSeries: typeof times; -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 const tryEach: typeof race; +export function transform(arr: T[], iteratee: (acc: R[], item: T, key: number, callback: ErrorCallback) => void, callback: AsyncResultArrayCallback): void; +export function transform(arr: T[], iteratee: (acc: R[], item: T, key: number, callback: ErrorCallback) => void): Promise; +export function transform(arr: T[], acc: R, iteratee: (acc: R, item: T, key: number, callback: ErrorCallback) => void, callback: AsyncResultCallback): void; +export function transform(arr: T[], acc: R, iteratee: (acc: R, item: T, key: number, callback: ErrorCallback) => void): Promise; + +export function transform( + arr: { [key: string]: T }, + iteratee: (acc: { [key: string]: R }, item: T, key: string, callback: ErrorCallback) => void, + callback: AsyncResultObjectCallback +): 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; + arr: { [key: string]: T }, + iteratee: (acc: { [key: string]: R }, item: T, key: string, callback: ErrorCallback) => void, +): Promise>; 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; + arr: { [key: string]: T }, + acc: R, + iteratee: (acc: R, item: T, key: string, callback: ErrorCallback) => void, + callback: AsyncResultCallback +): void; +export function transform( + arr: { [key: string]: T }, + acc: R, + iteratee: (acc: R, item: T, key: string, callback: ErrorCallback) => void, +): Promise; export function race(tasks: Array>, callback: AsyncResultCallback): void; +export function race(tasks: Array>): Promise; // 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 ensureAsync(fn: (...argsAndCallback: any[]) => void): Function; export function constant(...values: any[]): Function; export function asyncify(fn: Function): (...args: any[]) => any; export function wrapSync(fn: Function): Function; diff --git a/types/async/test/index.ts b/types/async/test/index.ts index 7516e491921b7a..327d0cdd39c287 100644 --- a/types/async/test/index.ts +++ b/types/async/test/index.ts @@ -9,7 +9,7 @@ declare var path: { exists: (path: string, callback?: (err: Error, exists: boolean) => any) => void; }; -function funcStringCbErrBoolean(v: string, cb: (err: Error, res: boolean) => void) {} +function funcStringCbErrBoolean(v: string, cb: (err: Error, res: boolean) => void) { } function callback() { } async.map(['file1', 'file2', 'file3'], fs.stat, (err: Error, results: fs.Stats[]) => { }); @@ -103,15 +103,15 @@ async.series([callback => { callback(undefined, 'one'); }, callback => { callbac async.series([callback => { callback(undefined, 'one'); }, callback => { callback(undefined, 'two'); }], (err, results) => { }); async.series({ - one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, - two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } - }, + one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, + two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } +}, (err, results) => { }); async.series({ - one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, - two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } - }, + one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, + two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } +}, (err, results) => { }); async.times(5, (n, next) => { next(undefined as any, n); }, (err, results) => { console.log(results); }); @@ -119,38 +119,38 @@ async.times(5, (n, next) => { next(undefined as any, n); }, (err, results) => { async.timesSeries(5, (n, next) => { next(undefined as any, n); }, (err, results) => { console.log(results); }); async.parallel([ - callback => { setTimeout(() => { callback(undefined, 'one'); }, 200); }, - callback => { setTimeout(() => { callback(undefined, 'two'); }, 100); } - ], + callback => { setTimeout(() => { callback(undefined, 'one'); }, 200); }, + callback => { setTimeout(() => { callback(undefined, 'two'); }, 100); } +], (err, results) => { }); async.parallel([ - callback => { setTimeout(() => { callback(undefined, 'one'); }, 200); }, - callback => { setTimeout(() => { callback(undefined, 'two'); }, 100); } - ], + callback => { setTimeout(() => { callback(undefined, 'one'); }, 200); }, + callback => { setTimeout(() => { callback(undefined, 'two'); }, 100); } +], (err, results) => { }); async.parallel({ - one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, - two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } - }, + one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, + two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } +}, (err, results) => { }); async.parallel({ - one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, - two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } - }, + one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, + two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } +}, (err, results) => { }); async.parallelLimit({ - one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, - two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } - }, + one: callback => { setTimeout(() => { callback(undefined, 1); }, 200); }, + two: callback => { setTimeout(() => { callback(undefined, 2); }, 100); } +}, 2, (err, results) => { } ); -function whileFn(callback: any) { +function whileFn(callback: (err: Error | null, result: number) => void) { setTimeout(() => callback(null, ++count), 1000); } @@ -164,14 +164,14 @@ async.doWhilst(whileFn, doWhileTest, err => { }); async.doUntil(whileFn, doWhileTest, err => { }); async.during(testCallback => { testCallback(new Error(), false); }, callback => { callback(); }, error => { console.log(error); }); -async.doDuring(callback => { callback(); }, testCallback => { testCallback(new Error(), false); }, error => { console.log(error); }); +async.doDuring(callback => { callback(); }, (testCallback: (err: Error | null, truth: boolean) => void) => { testCallback(new Error(), false); }, error => { console.log(error); }); async.forever(errBack => { errBack(new Error("Not going on forever.")); }, error => { console.log(error); }); async.waterfall([ - (callback: any) => { callback(null, 'one', 'two'); }, - (arg1: any, arg2: any, callback: any) => { callback(null, 'three'); }, - (arg1: any, callback: any) => { callback(null, 'done'); } - ], + (callback: any) => { callback(null, 'one', 'two'); }, + (arg1: any, arg2: any, callback: any) => { callback(null, 'three'); }, + (arg1: any, callback: any) => { callback(null, 'done'); } +], (err, result) => { }); const q = async.queue((task: any, callback: (err?: Error, msg?: string) => void) => { @@ -184,7 +184,7 @@ q.drain(() => { console.log('all items have been processed'); }); q.push({ name: 'foo' }); q.push({ name: 'bar' }, err => { console.log('finished processing bar'); }); q.push([{ name: 'baz' }, { name: 'bay' }, { name: 'bax' }], err => { console.log('finished processing bar'); }); -q.push({name: 'foo'}, (err, msg) => { console.log(`foo finished with a message "${msg!}"`); }); +q.push({ name: 'foo' }, (err, msg) => { console.log(`foo finished with a message "${msg!}"`); }); q.unshift({ name: 'foo' }); q.unshift({ name: 'bar' }, err => { console.log('finished processing bar'); }); @@ -225,7 +225,7 @@ q2.push('testRemovalTask'); q2.remove(x => x.data === 'testTaskRemoval'); if (q2Length !== q2.length()) { - console.log('warning: Failed to remove a task from queue.'); + console.log('warning: Failed to remove a task from queue.'); } const aq = async.queue((level: number, callback: (error?: Error, newLevel?: number) => void) => { @@ -243,7 +243,7 @@ const q3 = async.queue((task: string, callback: ErrorCallback) => { }, 1); q3.error((error, task) => { - console.log('task: ' + task); + console.log('task: ' + task); console.log('error: ' + error); }); @@ -277,15 +277,15 @@ async.auto({ }); async.auto({ - get_data: (callback: AsyncResultCallback) => { }, - make_folder: (callback: AsyncResultCallback) => { }, + get_data: (callback: AsyncResultCallback) => { }, + make_folder: (callback: AsyncResultCallback) => { }, - // arrays with different types are not accepted by TypeScript. - write_file: ['get_data', 'make_folder', ((callback: AsyncResultCallback) => { callback(null, filename); }) as any], + // arrays with different types are not accepted by TypeScript. + write_file: ['get_data', 'make_folder', ((callback: AsyncResultCallback) => { callback(null, filename); }) as any], - // arrays with different types are not accepted by TypeScript. - email_link: ['write_file', ((callback: AsyncResultCallback, results: any) => { }) as any] - }, + // arrays with different types are not accepted by TypeScript. + email_link: ['write_file', ((callback: AsyncResultCallback, results: any) => { }) as any] +}, (err, results) => { console.log('finished auto'); } ); @@ -297,30 +297,31 @@ interface A { } async.auto({ - get_data: (callback: AsyncResultCallback) => { }, - make_folder: (callback: AsyncResultCallback) => { }, + get_data: (callback: AsyncResultCallback) => { }, + make_folder: (callback: AsyncResultCallback) => { }, - // arrays with different types are not accepted by TypeScript. - write_file: ['get_data', 'make_folder', ((callback: AsyncResultCallback) => { callback(null, filename); }) as any], + // arrays with different types are not accepted by TypeScript. + write_file: ['get_data', 'make_folder', ((callback: AsyncResultCallback) => { callback(null, filename); }) as any], - // arrays with different types are not accepted by TypeScript. - email_link: ['write_file', ((callback: AsyncResultCallback, results: any) => { }) as any] - }, + // arrays with different types are not accepted by TypeScript. + email_link: ['write_file', ((callback: AsyncResultCallback, results: any) => { }) as any] +}, 1, (err, results) => { console.log('finished auto'); } ); -async.retry(3, (callback, results) => { }, (err, result) => { }); -async.retry({ times: 3, interval: 200 }, (callback, results) => { }, (err, result) => { }); -async.retry({ times: 3, interval: (retryCount) => 200 * retryCount }, (callback, results) => { }, (err, result) => { }); -async.retry({ times: 3, interval: 200, errorFilter: (err) => true }, (callback, results) => { }, (err, result) => { }); +async.retry(3, (callback) => { }, (err, result) => { }); +async.retry({ times: 3, interval: 200 }, (callback) => { }, (err, result) => { }); +async.retry({ times: 3, interval: (retryCount) => 200 * retryCount }, (callback) => { }, (err, result) => { }); +async.retry({ times: 3, interval: 200, errorFilter: (err) => true }, (callback) => { }, (err, result) => { }); async.parallel([ - (callback: (err: Error, val: string) => void) => { }, - callback => { } - ], - (err: Error, results: string[]) => { async.series([callback => { }, function email_link(callback) { }]); -}); + (callback: (err: Error, val: string) => void) => { }, + callback => { } +], + (err: Error, results: string[]) => { + async.series([callback => { }, function email_link(callback) { }]); + }); async.parallel([ async.apply(fs.writeFile, 'testfile1', 'test1'), @@ -360,7 +361,7 @@ async.each( console.log(`async.each: ${val}`); next(); }, - 500); + 500); }, (err?: Error) => { console.log("async.each: done."); } ); @@ -386,7 +387,7 @@ async.eachLimit( console.log(`async.eachLimit: ${val}`); next(); }, - 500); + 500); }, (err?: Error) => { console.log("async.eachLimit: done."); } ); @@ -465,12 +466,12 @@ async.mapLimit( { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 }, 2, (val: number, next: AsyncResultCallback) => { - setTimeout( - () => { - console.log(`async.mapLimit: ${val}`); - next(undefined as any, val.toString()); - }, - 500); + setTimeout( + () => { + console.log(`async.mapLimit: ${val}`); + next(undefined as any, val.toString()); + }, + 500); }, (err: Error, results: string[]) => { console.log("async.mapLimit: done with results", results); } ); @@ -584,24 +585,46 @@ async.some( // timeout function myFunction1(foo: any, callback: (err?: Error, result?: any) => void): void { - console.log(`async.timeout 1 ${foo}`); - callback(undefined, foo); + console.log(`async.timeout 1 ${foo}`); + callback(undefined, foo); } const wrapped1 = async.timeout(myFunction1, 1000); wrapped1({ bar: 'bar' }, (err: Error, data: any) => { console.log(`async.timeout 1 end ${data}`); }); function myFunction2(callback: (err?: Error, result?: any) => void): void { - console.log(`async.timeout 2`); - callback(undefined, { bar: 'bar' }); + console.log(`async.timeout 2`); + callback(undefined, { bar: 'bar' }); } const wrapped2 = async.timeout(myFunction2, 1000); wrapped2((err: Error, data: any) => { console.log(`async.timeout 2 end ${data}`); }); function myFunction3(callback: (err?: Error, result?: any) => void): void { - console.log(`async.timeout 3`); - callback(undefined, { bar: 'bar' }); + console.log(`async.timeout 3`); + callback(undefined, { bar: 'bar' }); } const wrapped3 = async.timeout(myFunction3, 1000, { bar: 'bar' }); wrapped3((err: Error, data: any) => { console.log(`async.timeout 3 end ${data}`); }); + +async.race([ + (callback) => { + callback(null, 42); + }, + (callback) => { + callback(null, 'foo'); + } +], (err, result) => { + console.log(result); +}); + +async.race([ + (callback) => { + callback(null, 42); + }, + (callback) => { + callback(null, 'foo'); + } +]).then(result => { + console.log(result); +}); diff --git a/types/async/tsconfig.json b/types/async/tsconfig.json index 153a5471fbd461..fe0e1074b86b69 100644 --- a/types/async/tsconfig.json +++ b/types/async/tsconfig.json @@ -23,4 +23,4 @@ "test/explicit.ts", "test/es6-generators.ts" ] -} \ No newline at end of file +}