Skip to content

Commit 25a2b0a

Browse files
committed
Merge pull request DefinitelyTyped#4602 from mnahkies/when-settle
when.js - Add definition with test for when.settle, add a test for when.all
2 parents 28fb135 + eb5f7c2 commit 25a2b0a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

when/when-tests.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ promise = liftedFunc5(when(1), when('2'), when(true), when(4), when('5'));
9191

9292
var joinedPromise: when.Promise<number[]> = when.join(when(1), when(2), when(3));
9393

94+
/* when.all(arr) */
95+
when.all<number[]>([when(1), when(2), when(3)]).then(results => {
96+
return results.reduce((r, x) => r + x, 0);
97+
});
98+
99+
/* when.settle(arr) */
100+
when.settle<number>([when(1), when(2), when.reject(new Error("Foo"))]).then(descriptors => {
101+
return descriptors.filter(d => d.state === 'rejected').reduce((r, d) => r + d.value, 0);
102+
});
103+
94104
/* when.promise(resolver) */
95105

96106
promise = when.promise<number>(resolve => resolve(5));

when/when.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,32 @@ declare module When {
101101
*/
102102
function all<T>(promisesOrValues: any[]): Promise<T>;
103103

104+
/**
105+
* Describes the status of a promise.
106+
* state may be one of:
107+
* "fulfilled" - the promise has resolved
108+
* "pending" - the promise is still pending to resolve/reject
109+
* "rejected" - the promise has rejected
110+
*/
111+
interface Descriptor<T> {
112+
state: string;
113+
value?: T;
114+
reason?: any;
115+
}
116+
117+
/**
118+
* Returns a promise for an array containing the same number of elements as the input array.
119+
* Each element is a descriptor object describing of the outcome of the corresponding element in the input.
120+
* The returned promise will only reject if array itself is a rejected promise. Otherwise,
121+
* it will always fulfill with an array of descriptors. This is in contrast to when.all,
122+
* which will reject if any element of array rejects.
123+
* @memberOf when
124+
*
125+
* @param promisesOrValues array of anything, may contain a mix
126+
* of {@link Promise}s and values
127+
*/
128+
function settle<T>(promisesOrValues: any[]): Promise<Descriptor<T>[]>;
129+
104130
/**
105131
* Creates a {promise, resolver} pair, either or both of which
106132
* may be given out safely to consumers.

0 commit comments

Comments
 (0)