From 33ac73d1f48315d0f237db4b8dde1d4ae6db3d38 Mon Sep 17 00:00:00 2001 From: Pascal Martin Date: Wed, 18 Jan 2017 13:23:09 -0500 Subject: [PATCH] Make timeout info param optional (#14034) Add support for timeout with AsyncResulIterator Add support for queue with AsyncResullIterator --- async/index.d.ts | 8 ++++++-- async/test/index.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/async/index.d.ts b/async/index.d.ts index 86aa17ea6b..e3fbe7e512 100644 --- a/async/index.d.ts +++ b/async/index.d.ts @@ -1,6 +1,6 @@ // Type definitions for Async 2.0.1 // Project: https://github.com/caolan/async -// Definitions by: Boris Yankov , Arseniy Maximov , Joe Herman , Angus Fenying +// Definitions by: Boris Yankov , Arseniy Maximov , Joe Herman , Angus Fenying , Pascal Martin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped interface Dictionary { [key: string]: T; } @@ -29,6 +29,7 @@ interface AsyncQueue { idle(): boolean; concurrency: number; push(task: T, callback?: ErrorCallback): void; + push(task: T, callback?: AsyncResultCallback): void; push(task: T[], callback?: ErrorCallback): void; unshift(task: T, callback?: ErrorCallback): void; unshift(task: T[], callback?: ErrorCallback): void; @@ -180,6 +181,7 @@ interface Async { applyEach(fns: Function[], argsAndCallback: any[]): void; // applyEach(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. applyEachSeries(fns: Function[], argsAndCallback: any[]): void; // applyEachSeries(fns, args..., callback). TS does not support ... for a middle argument. Callback is optional. queue(worker: AsyncWorker, concurrency?: number): AsyncQueue; + queue(worker: AsyncResultIterator, concurrency?: number): AsyncQueue; priorityQueue(worker: AsyncWorker, concurrency: number): AsyncPriorityQueue; cargo(worker : (tasks: any[], callback : ErrorCallback) => void, payload? : number) : AsyncCargo; auto(tasks: any, concurrency?: number, callback?: AsyncResultCallback): void; @@ -194,7 +196,8 @@ interface Async { reflect(fn: AsyncFunction) : (callback: (err: void, result: {error?: Error, value?: T}) => void) => void; reflectAll(tasks: AsyncFunction[]): ((callback: (err: void, result: {error?: Error, value?: T}) => void) => void)[]; - timeout(fn: AsyncFunction, milliseconds: number, info: any): AsyncFunction; + timeout(fn: AsyncFunction, milliseconds: number, info?: any): AsyncFunction; + timeout(fn: AsyncResultIterator, milliseconds: number, info?: any): AsyncResultIterator; times (n: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; timesSeries(n: number, iterator: AsyncResultIterator, callback: AsyncResultArrayCallback): void; @@ -223,3 +226,4 @@ declare var async: Async; declare module "async" { export = async; } + diff --git a/async/test/index.ts b/async/test/index.ts index abc2653f60..8c6be303e4 100644 --- a/async/test/index.ts +++ b/async/test/index.ts @@ -349,6 +349,16 @@ q2.unshift(['task3', 'task4', 'task5'], function (error) { console.log('Finished tasks'); }); + +var aq = async.queue(function (level: number, callback: (error : Error, newLevel: number) => void) { + console.log('hello ' + level); + callback(null, level+1); +}); + +aq.push(1, function (err : Error, newLevel : number) { + console.log('finished processing bar' + newLevel); +}); + // create a cargo object with payload 2 var cargo = async.cargo(function (tasks, callback) { for (var i = 0; i < tasks.length; i++) { @@ -794,3 +804,36 @@ async.some({ console.log("async.some/any: done with result", result); }); + +// timeout + +function myFunction1(foo : any, callback: (err : Error, result : any) => void ) : void { + console.log(`async.timeout 1 ${foo}`); + return callback(null, foo); +} +var wrapped1 = async.timeout(myFunction1, 1000); +wrapped1({ bar: 'bar' }, function(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`); + return callback(null, { bar: 'bar' }); +} + +var wrapped2 = async.timeout(myFunction2, 1000); +wrapped2( function(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`); + return callback(null, { bar: 'bar' }); +} + +var wrapped3 = async.timeout(myFunction3, 1000, { bar: 'bar' }); +wrapped3( function(err : Error, data : any) { + console.log(`async.timeout 3 end ${data}`); +}); +