Skip to content

Commit 6401632

Browse files
committed
feat: add settleAll with types
@W-12128179@
1 parent c7d5b7e commit 6401632

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './json';
1313
export * from './nodash';
1414
export * from './collections';
1515
export * from './throttledPromiseAll';
16+
export * from './settleAll';

src/settleAll.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
export type SettledResult<T, E = Error> = {
8+
fulfilled: T[];
9+
rejected: E[];
10+
};
11+
12+
export async function settleAll<T, E = Error>(promises: Array<Promise<T>>): Promise<SettledResult<T, E>> {
13+
const settled: SettledResult<T, E> = {
14+
fulfilled: [],
15+
rejected: [],
16+
};
17+
18+
const allSettled = await Promise.allSettled(promises);
19+
settled.fulfilled = allSettled
20+
.filter((s) => s.status === 'fulfilled')
21+
.map((s) => {
22+
if (s.status === 'fulfilled') {
23+
return s.value as T;
24+
} else {
25+
return undefined;
26+
}
27+
})
28+
.filter((v) => v !== undefined) as T[];
29+
30+
settled.rejected = allSettled
31+
.filter((s) => s.status === 'rejected')
32+
.map((s) => {
33+
if (s.status === 'rejected') {
34+
return s.reason as E;
35+
} else {
36+
return undefined;
37+
}
38+
})
39+
.filter((v) => v !== undefined) as E[];
40+
41+
return settled;
42+
}

test/settleAll.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2023, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { expect } from 'chai';
8+
import { settleAll } from '../src';
9+
10+
describe('settleAll', () => {
11+
it('should fulfill all promises', async () => {
12+
const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
13+
const settled = await settleAll(promises);
14+
expect(settled.fulfilled).to.deep.equal([1, 2, 3]);
15+
expect(settled.rejected).to.deep.equal([]);
16+
});
17+
it('should reject all promises', async () => {
18+
const promises = [Promise.reject(1), Promise.reject(2), Promise.reject(3)];
19+
const settled = await settleAll(promises);
20+
expect(settled.fulfilled).to.deep.equal([]);
21+
expect(settled.rejected).to.deep.equal([1, 2, 3]);
22+
});
23+
it('should fulfill and reject promises', async () => {
24+
const promises = [Promise.resolve(1), Promise.reject(2), Promise.resolve(3)];
25+
const settled = await settleAll(promises);
26+
expect(settled.fulfilled).to.deep.equal([1, 3]);
27+
expect(settled.rejected).to.deep.equal([2]);
28+
});
29+
it('should handle empty array', async () => {
30+
const promises: Array<Promise<never>> = [];
31+
const settled = await settleAll(promises);
32+
expect(settled.fulfilled).to.deep.equal([]);
33+
expect(settled.rejected).to.deep.equal([]);
34+
});
35+
// types other than primitives
36+
it('should handle types other than primitives', async () => {
37+
const promises = [Promise.resolve({ a: 1 }), Promise.resolve({ b: 2 }), Promise.resolve({ c: 3 })];
38+
const settled = await settleAll<Record<string, number>>(promises);
39+
expect(settled.fulfilled).to.deep.equal([{ a: 1 }, { b: 2 }, { c: 3 }]);
40+
expect(settled.rejected).to.deep.equal([]);
41+
});
42+
it('should handle types other than primitives and reject', async () => {
43+
const promises = [Promise.resolve({ a: 1 }), Promise.reject(new Error('boo')), Promise.resolve({ c: 3 })];
44+
const settled = await settleAll<Record<string, number>>(promises);
45+
expect(settled.fulfilled).to.deep.equal([{ a: 1 }, { c: 3 }]);
46+
expect(settled.rejected[0].message).to.deep.equal('boo');
47+
});
48+
});

0 commit comments

Comments
 (0)