add when.reduce, when.reduceRight, and when.map typings (#11894)

This commit is contained in:
Cain Cresswell-Miley
2016-10-12 21:57:24 +09:00
committed by Masahiro Wakame
parent 2361fca3be
commit befa2dae51
2 changed files with 54 additions and 0 deletions
+20
View File
@@ -96,6 +96,26 @@ when.all<number[]>([when(1), when(2), when(3)]).then(results => {
return results.reduce((r, x) => r + x, 0);
});
/* when.map(arr, fn) */
when.map<number[]>([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<number>([when(1), 2, 3], (reduction: number, value: number) => {
return reduction += value;
}, 0).then((result: number) => {
return result;
});
/* when.reduceRight(arr, reduceFunc, initialValue) */
when.reduceRight<number>([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<number>([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);
+34
View File
@@ -101,6 +101,40 @@ declare namespace When {
*/
function all<T>(promisesOrValues: any[]): Promise<T>;
/**
* 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<T>(promisesOrValues: any[], mapFunc: (value: any, index?: Number) => any): Promise<T>;
/**
* 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<T>(promisesOrValues: any[], reduceFunc: (reduction: T, value: any, index?: Number) => T | Promise<T>, initialValue: T): Promise<T>;
/**
* 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<T>(promisesOrValues: any[], reduceFunc: (reduction: T, value: any, index?: Number) => T | Promise<T>, initialValue: T): Promise<T>;
/**
* Describes the status of a promise.
* state may be one of: