Skip to content

Commit 96511b1

Browse files
committed
Handle rejections properly
1 parent 60f063e commit 96511b1

23 files changed

+274
-84
lines changed

src/every.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@ import asyncForEach from './forEach';
22

33
export default function asyncSome(arr, fn) {
44
let resolved;
5-
return new Promise((resolve) => {
5+
return new Promise((resolve, reject) => {
66
asyncForEach(
77
arr,
88
(cur, idx, arr2) =>
9-
new Promise((resolve2) => {
9+
new Promise((resolve2, reject2) => {
1010
if (resolved) {
1111
return;
1212
}
13-
fn(cur, idx, arr2).then((result) => {
14-
if (!result) {
15-
resolve(false);
16-
resolved = true;
17-
}
18-
resolve2();
19-
});
13+
fn(cur, idx, arr2)
14+
.then((result) => {
15+
if (!result) {
16+
resolve(false);
17+
resolved = true;
18+
}
19+
resolve2();
20+
})
21+
.catch(reject2);
2022
}),
21-
).then(() => {
22-
resolve(true);
23-
resolved = true;
24-
});
23+
)
24+
.then(() => {
25+
resolve(true);
26+
resolved = true;
27+
})
28+
.catch((error) => {
29+
reject(error);
30+
resolved = true;
31+
});
2532
});
2633
}

src/every.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
largerThanOneHundred,
77
largerThanOneHundredInRandomTime,
88
makeDelayed,
9+
throws,
910
} from '../test-utils';
1011

1112
function largerOrEqualThanZero(x) {
@@ -102,4 +103,16 @@ describe('asyncEvery()', () => {
102103

103104
expect(result).toEqual(false);
104105
});
106+
107+
it.skip('assertions below are valid for synchronous .every()', () => {
108+
const mapper = jest.fn().mockImplementation(throws);
109+
110+
expect(() => inputArr.every(mapper)).toThrow('Some error');
111+
});
112+
113+
it('throws if function passed throws', async () => {
114+
const mapper = jest.fn().mockImplementation(throws);
115+
116+
await expect(() => asyncEvery(inputArr, mapper)).rejects.toThrow('Some error');
117+
});
105118
});

src/every_strict.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,32 @@ import asyncForEachStrict from './forEach_strict';
22

33
export default function asyncEveryStrict(arr, fn) {
44
let resolved;
5-
return new Promise((resolve) => {
5+
return new Promise((resolve, reject) => {
66
asyncForEachStrict(
77
arr,
88
(cur, idx, arr2) =>
9-
new Promise((resolve2) => {
9+
new Promise((resolve2, reject2) => {
1010
if (resolved) {
1111
return;
1212
}
13-
fn(cur, idx, arr2).then((result) => {
14-
if (!result) {
15-
resolve(false);
16-
resolved = true;
17-
}
18-
resolve2();
19-
});
13+
fn(cur, idx, arr2)
14+
.then((result) => {
15+
if (!result) {
16+
resolve(false);
17+
resolved = true;
18+
}
19+
resolve2();
20+
})
21+
.catch(reject2);
2022
}),
21-
).then(() => {
22-
resolve(true);
23-
resolved = true;
24-
});
23+
)
24+
.then(() => {
25+
resolve(true);
26+
resolved = true;
27+
})
28+
.catch((error) => {
29+
reject(error);
30+
resolved = true;
31+
});
2532
});
2633
}

src/every_strict.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
makeDelayed,
1010
makePushDuplicate,
1111
makePushDuplicateInRandomTime,
12+
throws,
1213
} from '../test-utils';
1314

1415
function largerOrEqualThanZero(x) {
@@ -137,4 +138,16 @@ describe('asyncEveryStrict()', () => {
137138

138139
expect(result).toEqual(false);
139140
});
141+
142+
it.skip('assertions below are valid for synchronous .every()', () => {
143+
const mapper = jest.fn().mockImplementation(throws);
144+
145+
expect(() => inputArr.every(mapper)).toThrow('Some error');
146+
});
147+
148+
it('throws if function passed throws', async () => {
149+
const mapper = jest.fn().mockImplementation(throws);
150+
151+
await expect(() => asyncEveryStrict(inputArr, mapper)).rejects.toThrow('Some error');
152+
});
140153
});

src/filter.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ export default function asyncSome(arr, fn) {
66
return asyncForEach(
77
arr,
88
(cur, idx, arr2) =>
9-
new Promise((resolve) => {
10-
fn(cur, idx, arr2).then((cond) => {
11-
if (cond) {
12-
result[idx] = cur;
13-
}
14-
resolve();
15-
});
9+
new Promise((resolve, reject) => {
10+
fn(cur, idx, arr2)
11+
.then((cond) => {
12+
if (cond) {
13+
result[idx] = cur;
14+
}
15+
resolve();
16+
})
17+
.catch(reject);
1618
}),
1719
).then(() => result.filter((cur, idx) => idx in result));
1820
}

src/filter.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
largerThanTwo,
77
largerThanTwoInRandomTime,
88
makeDelayed,
9+
throws,
910
} from '../test-utils';
1011

1112
describe('asyncFilter()', () => {
@@ -74,4 +75,16 @@ describe('asyncFilter()', () => {
7475

7576
expect(result).toEqual([3, 4, 5, 6, 7, 8, 9, 10]);
7677
});
78+
79+
it.skip('assertions below are valid for synchronous .filter()', () => {
80+
const mapper = jest.fn().mockImplementation(throws);
81+
82+
expect(() => inputArr.filter(mapper)).toThrow('Some error');
83+
});
84+
85+
it('throws if function passed throws', async () => {
86+
const mapper = jest.fn().mockImplementation(throws);
87+
88+
await expect(() => asyncFilter(inputArr, mapper)).rejects.toThrow('Some error');
89+
});
7790
});

src/filter_strict.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ export default function asyncFilterStrict(arr, fn) {
66
return asyncForEachStrict(
77
arr,
88
(cur, idx, arr2) =>
9-
new Promise((resolve) => {
10-
fn(cur, idx, arr2).then((cond) => {
11-
if (cond) {
12-
result[idx] = cur;
13-
}
14-
resolve();
15-
});
9+
new Promise((resolve, reject) => {
10+
fn(cur, idx, arr2)
11+
.then((cond) => {
12+
if (cond) {
13+
result[idx] = cur;
14+
}
15+
resolve();
16+
})
17+
.catch(reject);
1618
}),
1719
).then(() => result.filter((cur, idx) => idx in result));
1820
}

src/filter_strict.spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
makeDelayed,
1010
makePushDuplicate,
1111
makePushDuplicateInRandomTime,
12+
throws,
1213
} from '../test-utils';
1314

1415
describe('asyncFilterStrict()', () => {
@@ -68,7 +69,7 @@ describe('asyncFilterStrict()', () => {
6869
});
6970
});
7071

71-
it.skip('assertions below are valid for synchronous .map()', () => {
72+
it.skip('assertions below are valid for synchronous .filter()', () => {
7273
const [arr, pushDuplicate] = makePushDuplicate();
7374
const mapper = jest.fn().mockImplementation(pushDuplicate);
7475

@@ -103,4 +104,16 @@ describe('asyncFilterStrict()', () => {
103104

104105
expect(result).toEqual([3, 4, 5, 6, 7, 8, 9, 10]);
105106
});
107+
108+
it.skip('assertions below are valid for synchronous .filter()', () => {
109+
const mapper = jest.fn().mockImplementation(throws);
110+
111+
expect(() => inputArr.filter(mapper)).toThrow('Some error');
112+
});
113+
114+
it('throws if function passed throws', async () => {
115+
const mapper = jest.fn().mockImplementation(throws);
116+
117+
await expect(() => asyncFilterStrict(inputArr, mapper)).rejects.toThrow('Some error');
118+
});
106119
});

src/forEach.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import asyncMap from './map';
22

33
export default function asyncForEach(arr, fn) {
4-
return new Promise((resolve) => {
5-
asyncMap(arr, fn).then(() => resolve());
4+
return new Promise((resolve, reject) => {
5+
asyncMap(arr, fn)
6+
.then(() => resolve())
7+
.catch(reject);
68
});
79
}

src/forEach.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
makeDelayed,
77
makePushDuplicate,
88
makePushDuplicateInRandomTime,
9+
throws,
910
} from '../test-utils';
1011

1112
describe('asyncForEach()', () => {
@@ -85,4 +86,16 @@ describe('asyncForEach()', () => {
8586

8687
expect(result).toBe(undefined);
8788
});
89+
90+
it.skip('assertions below are valid for synchronous .forEach()', () => {
91+
const mapper = jest.fn().mockImplementation(throws);
92+
93+
expect(() => inputArr.forEach(mapper)).toThrow('Some error');
94+
});
95+
96+
it('throws if function passed throws', async () => {
97+
const mapper = jest.fn().mockImplementation(throws);
98+
99+
await expect(() => asyncForEach(inputArr, mapper)).rejects.toThrow('Some error');
100+
});
88101
});

0 commit comments

Comments
 (0)