Skip to content

Commit da053d5

Browse files
committed
Remove filterAsync method
1 parent bca8884 commit da053d5

File tree

2 files changed

+0
-64
lines changed

2 files changed

+0
-64
lines changed

packages/utils/src/async.ts

-17
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,3 @@ export function forget(promise: Promise<any>): void {
88
console.error(e);
99
});
1010
}
11-
12-
/**
13-
* Helper to filter an array with asynchronous callbacks.
14-
*
15-
* @param array An array containing items to filter.
16-
* @param predicate An async predicate evaluated on every item.
17-
* @param thisArg Optional value passed as "this" into the callback.
18-
* @returns An array containing only values where the callback returned true.
19-
*/
20-
export async function filterAsync<T>(
21-
array: T[],
22-
predicate: (item: T) => Promise<boolean> | boolean,
23-
thisArg?: any,
24-
): Promise<T[]> {
25-
const verdicts = await Promise.all(array.map(predicate, thisArg));
26-
return array.filter((_, index) => verdicts[index]);
27-
}

packages/utils/test/async.test.ts

-47
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,3 @@ describe('forget', () => {
2020
});
2121
});
2222
});
23-
24-
describe('filterAsync', () => {
25-
test('filters with sync predicate', async () => {
26-
expect.assertions(1);
27-
const filtered = await filterAsync([1, 2, 3, 4], i => i > 2);
28-
expect(filtered).toEqual([3, 4]);
29-
});
30-
31-
test('filters with async predicate', async () => {
32-
expect.assertions(1);
33-
34-
const predicate = async (i: number) =>
35-
new Promise<boolean>(resolve =>
36-
setTimeout(() => {
37-
resolve(i > 2);
38-
}, i * 100),
39-
);
40-
41-
const filtered = await filterAsync([1, 2, 3, 4], predicate);
42-
expect(filtered).toEqual([3, 4]);
43-
});
44-
45-
test('passes filter arguments to the predicate', async () => {
46-
expect.assertions(1);
47-
48-
const arr = [1];
49-
const predicate = jest.fn();
50-
51-
await filterAsync(arr, predicate);
52-
expect(predicate).toHaveBeenCalledWith(1, 0, arr);
53-
});
54-
55-
test('passes this to the predicate', async () => {
56-
expect.assertions(1);
57-
58-
const that = {};
59-
await filterAsync(
60-
[1],
61-
function predicate(this: {}): boolean {
62-
// tslint:disable-next-line:no-inferred-empty-object-type
63-
expect(this).toBe(that);
64-
return false;
65-
},
66-
that,
67-
);
68-
});
69-
});

0 commit comments

Comments
 (0)