Add definition with test for when.settle, add a test for when.all

This commit is contained in:
Michael Nahkies
2015-06-10 16:38:16 +12:00
parent 52dc8fc393
commit eb5f7c2d2a
2 changed files with 36 additions and 0 deletions
+10
View File
@@ -91,6 +91,16 @@ promise = liftedFunc5(when(1), when('2'), when(true), when(4), when('5'));
var joinedPromise: when.Promise<number[]> = when.join(when(1), when(2), when(3));
/* when.all(arr) */
when.all<number[]>([when(1), when(2), when(3)]).then(results => {
return results.reduce((r, x) => r + x, 0);
});
/* 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);
});
/* when.promise(resolver) */
promise = when.promise<number>(resolve => resolve(5));
+26
View File
@@ -101,6 +101,32 @@ declare module When {
*/
function all<T>(promisesOrValues: any[]): Promise<T>;
/**
* 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<T> {
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<T>(promisesOrValues: any[]): Promise<Descriptor<T>[]>;
/**
* Creates a {promise, resolver} pair, either or both of which
* may be given out safely to consumers.