From eb5f7c2d2a1ee0de72fe9d8b40c9d33ec2612e63 Mon Sep 17 00:00:00 2001 From: Michael Nahkies Date: Wed, 10 Jun 2015 16:38:16 +1200 Subject: [PATCH] Add definition with test for when.settle, add a test for when.all --- when/when-tests.ts | 10 ++++++++++ when/when.d.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/when/when-tests.ts b/when/when-tests.ts index 61b688a035..7f365b4cc1 100644 --- a/when/when-tests.ts +++ b/when/when-tests.ts @@ -91,6 +91,16 @@ promise = liftedFunc5(when(1), when('2'), when(true), when(4), when('5')); var joinedPromise: when.Promise = when.join(when(1), when(2), when(3)); +/* when.all(arr) */ +when.all([when(1), when(2), when(3)]).then(results => { + return results.reduce((r, x) => r + x, 0); +}); + +/* 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); +}); + /* when.promise(resolver) */ promise = when.promise(resolve => resolve(5)); diff --git a/when/when.d.ts b/when/when.d.ts index be5e25f154..ab44901de6 100644 --- a/when/when.d.ts +++ b/when/when.d.ts @@ -101,6 +101,32 @@ declare module When { */ function all(promisesOrValues: any[]): Promise; + /** + * Describes the status of a promise. + * state may be one of: + * "fulfilled" - the promise has resolved + * "pending" - the promise is still pending to resolve/reject + * "rejected" - the promise has rejected + */ + interface Descriptor { + state: string; + value?: T; + reason?: any; + } + + /** + * Returns a promise for an array containing the same number of elements as the input array. + * Each element is a descriptor object describing of the outcome of the corresponding element in the input. + * The returned promise will only reject if array itself is a rejected promise. Otherwise, + * it will always fulfill with an array of descriptors. This is in contrast to when.all, + * which will reject if any element of array rejects. + * @memberOf when + * + * @param promisesOrValues array of anything, may contain a mix + * of {@link Promise}s and values + */ + function settle(promisesOrValues: any[]): Promise[]>; + /** * Creates a {promise, resolver} pair, either or both of which * may be given out safely to consumers.