mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-16 06:50:19 +00:00
Add definition with test for when.settle, add a test for when.all
This commit is contained in:
@@ -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));
|
||||
|
||||
Vendored
+26
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user