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 2 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
15 changes: 15 additions & 0 deletions test.js
Expand Up @@ -107,3 +107,18 @@ 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) {
return delay.reject(100, {value: new Error('Oops!')});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but this could just await the delay and then throw the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! Done: fd0bb09

}
})
);
await delay(500);
t.deepEqual(mappedValues, [1, 3]);
});