Skip to content

Commit

Permalink
Do not run mapping after stop-on-error happened (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
papb committed Jun 27, 2021
1 parent 9e11914 commit 4b5f9e7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -53,6 +53,11 @@ export default async function pMap(
(async () => {
try {
const element = await nextItem.value;

if (isRejected) {
return;
}

result[index] = await mapper(element, index);
resolvingCount--;
next();
Expand Down
16 changes: 16 additions & 0 deletions test.js
Expand Up @@ -107,3 +107,19 @@ test('aggregate errors when stopOnError is false', async t => {
await t.throwsAsync(pMap(errorInput1, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: /foo(.|\n)*bar/});
await t.throwsAsync(pMap(errorInput2, mapper, {concurrency: 1, stopOnError: false}), {instanceOf: AggregateError, message: /bar(.|\n)*foo/});
});

test('do not run mapping after stop-on-error happened', async t => {
const input = [1, delay(300, {value: 2}), 3];
const mappedValues = [];
await t.throwsAsync(
pMap(input, async value => {
mappedValues.push(value);
if (value === 1) {
await delay(100);
throw new Error('Oops!');
}
})
);
await delay(500);
t.deepEqual(mappedValues, [1, 3]);
});

0 comments on commit 4b5f9e7

Please sign in to comment.