bluebird: Add 'each'

This commit is contained in:
mfrantz
2015-09-16 16:49:21 -07:00
parent 5f48028783
commit b1c0b5542d
2 changed files with 64 additions and 0 deletions

View File

@@ -550,6 +550,13 @@ fooArrProm = fooArrProm.filter<Foo>((item: Foo) => {
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fooArrProm = fooArrProm.each<Foo, Bar>((item: Foo): Bar => bar);
fooArrProm = fooArrProm.each<Foo, Bar>((item: Foo, index: number): Bar => index ? bar : null);
fooArrProm = fooArrProm.each<Foo, Bar>((item: Foo, index: number, arrayLength: number): Bar => bar);
fooArrProm = fooArrProm.each<Foo, Bar>((item: Foo, index: number, arrayLength: number): Promise<Bar> => barProm);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fooProm = Promise.try(() => {
return foo;
@@ -1123,3 +1130,43 @@ fooArrProm = Promise.filter(fooArr, (item: Foo, index: number, arrayLength: numb
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// each()
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// fooThenArrThen
fooArrThen = Promise.each(fooThenArrThen, (item: Foo) => bar);
fooArrThen = Promise.each(fooThenArrThen, (item: Foo) => barThen);
fooArrThen = Promise.each(fooThenArrThen, (item: Foo, index: number, arrayLength: number) => bar);
fooArrThen = Promise.each(fooThenArrThen, (item: Foo, index: number, arrayLength: number) => barThen);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// fooArrThen
fooArrThen = Promise.each(fooArrThen, (item: Foo) => bar);
fooArrThen = Promise.each(fooArrThen, (item: Foo) => barThen);
fooArrThen = Promise.each(fooArrThen, (item: Foo, index: number, arrayLength: number) => bar);
fooArrThen = Promise.each(fooArrThen, (item: Foo, index: number, arrayLength: number) => barThen);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// fooThenArr
fooArrThen = Promise.each(fooThenArr, (item: Foo) => bar);
fooArrThen = Promise.each(fooThenArr, (item: Foo) => barThen);
fooArrThen = Promise.each(fooThenArr, (item: Foo, index: number, arrayLength: number) => bar);
fooArrThen = Promise.each(fooThenArr, (item: Foo, index: number, arrayLength: number) => barThen);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// fooArr
fooArrThen = Promise.each(fooArr, (item: Foo) => bar);
fooArrThen = Promise.each(fooArr, (item: Foo) => barThen);
fooArrThen = Promise.each(fooArr, (item: Foo, index: number, arrayLength: number) => bar);
fooArrThen = Promise.each(fooArr, (item: Foo, index: number, arrayLength: number) => barThen);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@@ -328,6 +328,11 @@ declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R> {
filter<U>(filterer: (item: U, index: number, arrayLength: number) => Promise.Thenable<boolean>, options?: Promise.ConcurrencyOption): Promise<U[]>;
filter<U>(filterer: (item: U, index: number, arrayLength: number) => boolean, options?: Promise.ConcurrencyOption): Promise<U[]>;
/**
* Same as calling ``Promise.each(thisPromise, iterator)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
*/
each<R, U>(iterator: (item: R, index: number, arrayLength: number) => U | Promise.Thenable<U>): Promise<R[]>;
/**
* Start the chain of promises with `Promise.try`. Any synchronous exceptions will be turned into rejections on the returned promise.
*
@@ -607,6 +612,18 @@ declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R> {
// array with values
static filter<R>(values: R[], filterer: (item: R, index: number, arrayLength: number) => Promise.Thenable<boolean>, option?: Promise.ConcurrencyOption): Promise<R[]>;
static filter<R>(values: R[], filterer: (item: R, index: number, arrayLength: number) => boolean, option?: Promise.ConcurrencyOption): Promise<R[]>;
/**
* Iterate over an array, or a promise of an array, which contains promises (or a mix of promises and values) with the given iterator function with the signature (item, index, value) where item is the resolved value of a respective promise in the input array. Iteration happens in serially. If any promise in the input array is rejected the returned promise is rejected as well.
*
* Resolves to the original array unmodified, this method is meant to be used for side effects. If the iterator function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration.
*/
// promise of array with promises of value
static each<R, U>(values: Promise.Thenable<Promise.Thenable<R>[]>, iterator: (item: R, index: number, arrayLength: number) => U | Promise.Thenable<U>): Promise<R[]>;
// array with promises of value
static each<R, U>(values: Promise.Thenable<R>[], iterator: (item: R, index: number, arrayLength: number) => U | Promise.Thenable<U>): Promise<R[]>;
// array with values OR promise of array with values
static each<R, U>(values: R[] | Promise.Thenable<R[]>, iterator: (item: R, index: number, arrayLength: number) => U | Promise.Thenable<U>): Promise<R[]>;
}
declare module Promise {