From befa2dae51c784104b7de37300b931900861728c Mon Sep 17 00:00:00 2001 From: Cain Cresswell-Miley Date: Thu, 13 Oct 2016 01:57:24 +1300 Subject: [PATCH] add when.reduce, when.reduceRight, and when.map typings (#11894) --- when/when-tests.ts | 20 ++++++++++++++++++++ when/when.d.ts | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/when/when-tests.ts b/when/when-tests.ts index cdf252de88..41c4302870 100644 --- a/when/when-tests.ts +++ b/when/when-tests.ts @@ -96,6 +96,26 @@ when.all([when(1), when(2), when(3)]).then(results => { return results.reduce((r, x) => r + x, 0); }); +/* when.map(arr, fn) */ +when.map([when(1), 2, 3], (num: number, index: number) => num * index).then((results) => { + return results.reduce((r, x) => r + x, 0); +}); + +/* when.reduce(arr, reduceFunc, initialValue) */ +when.reduce([when(1), 2, 3], (reduction: number, value: number) => { + return reduction += value; +}, 0).then((result: number) => { + return result; +}); + +/* when.reduceRight(arr, reduceFunc, initialValue) */ +when.reduceRight([when(1), 2, 3], (reduction: number, value: number) => { + return when(value) + .then((v) => reduction += v); +}, 0).then((result: number) => { + return result; +}); + /* when.settle(arr) */ when.settle([when(1), when(2), when.reject(new Error("Foo"))]).then(descriptors => { return descriptors.filter(d => d.state === 'rejected').reduce((r, d) => r + d.value, 0); diff --git a/when/when.d.ts b/when/when.d.ts index 50a272ece8..a04411981b 100644 --- a/when/when.d.ts +++ b/when/when.d.ts @@ -101,6 +101,40 @@ declare namespace When { */ function all(promisesOrValues: any[]): Promise; + /** + * Promise-aware array map function, similar to `Array.prototype.map()`, + * but input array may contain promises or values. + * @param promisesOrValues array of anything, may contain a mix of {@link Promise}s and values + * @param mapFunc map function which may return a promise or value + * @returns a promise that will fulfill with an array of mapped values + * or reject if any input promise rejects. + */ + function map(promisesOrValues: any[], mapFunc: (value: any, index?: Number) => any): Promise; + + /** + * Traditional reduce function, similar to `Array.prototype.reduce()`, but + * input may contain promises and/or values, and reduceFunc + * may return either a value or a promise, *and* initialValue may + * be a promise for the starting value. + * @param promisesOrValues array or promise for an array of anything, + * may contain a mix of promises and values. + * @param reduceFunc function(accumulated:*, x:*, index:Number):*} f reduce function + * @returns a promise that will resolve to the final reduced value + */ + function reduce(promisesOrValues: any[], reduceFunc: (reduction: T, value: any, index?: Number) => T | Promise, initialValue: T): Promise; + + /** + * Traditional reduce function, similar to `Array.prototype.reduceRight()`, but + * input may contain promises and/or values, and reduceFunc + * may return either a value or a promise, *and* initialValue may + * be a promise for the starting value. + * @param promisesOrValues array or promise for an array of anything, + * may contain a mix of promises and values. + * @param reduceFunc function(accumulated:*, x:*, index:Number):*} f reduce function + * @returns a promise that will resolve to the final reduced value + */ + function reduceRight(promisesOrValues: any[], reduceFunc: (reduction: T, value: any, index?: Number) => T | Promise, initialValue: T): Promise; + /** * Describes the status of a promise. * state may be one of: