Skip to content

Commit 4b5f9e7

Browse files
authored
Do not run mapping after stop-on-error happened (#40)
1 parent 9e11914 commit 4b5f9e7

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export default async function pMap(
5353
(async () => {
5454
try {
5555
const element = await nextItem.value;
56+
57+
if (isRejected) {
58+
return;
59+
}
60+
5661
result[index] = await mapper(element, index);
5762
resolvingCount--;
5863
next();

test.js

+16
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,19 @@ test('aggregate errors when stopOnError is false', async t => {
107107
await t.throwsAsync(pMap(errorInput1, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: /foo(.|\n)*bar/});
108108
await t.throwsAsync(pMap(errorInput2, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: /bar(.|\n)*foo/});
109109
});
110+
111+
test('do not run mapping after stop-on-error happened', async t => {
112+
const input = [1, delay(300, {value: 2}), 3];
113+
const mappedValues = [];
114+
await t.throwsAsync(
115+
pMap(input, async value => {
116+
mappedValues.push(value);
117+
if (value === 1) {
118+
await delay(100);
119+
throw new Error('Oops!');
120+
}
121+
})
122+
);
123+
await delay(500);
124+
t.deepEqual(mappedValues, [1, 3]);
125+
});

0 commit comments

Comments
 (0)