Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not run mapping after stop-on-error happened #40

Merged
merged 3 commits into from Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]);
});