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

Small improvement of promise iteration error handling #58

Merged
merged 1 commit into from
Sep 4, 2024

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Sep 3, 2024

This is a tiny improvement of some logic related to handling errors thrown in a for await (const line of nanoSpawn(...)) { ... } loop.

}
};

const getNext = async iterator => {
try {
return await iterator.next();
} catch (error) {
await iterator.throw?.(error);
throw error;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

iterator is the lineIterator created for either stdout or stderr. This function combines both. If an error is thrown in the for loop, iterator.throw() for both stdout and stderr is called, then the original error is propagated.

The code works for any type of iterator. But in practice, we know that the iterator is always created by a generator function * () {}. That type of iterator:

  • Always has a return() and throw() functions (which are otherwise optional for other iterators)
  • Always has a throw() that re-throws the error passed as argument. Therefore, throw error is never reached.

So this PR does a small adjustment to remove a few bytes, by optimizing the fact that we know that iterator is coming from a generator function.

@sindresorhus sindresorhus merged commit 40c9f51 into main Sep 4, 2024
12 checks passed
@sindresorhus sindresorhus deleted the fix-iterable-error branch September 4, 2024 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants