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

Support a "left" constructor, for example in EitherAsync #727

Closed
kbirger opened this issue Aug 7, 2024 · 1 comment
Closed

Support a "left" constructor, for example in EitherAsync #727

kbirger opened this issue Aug 7, 2024 · 1 comment

Comments

@kbirger
Copy link

kbirger commented Aug 7, 2024

Currently, it looks like Left values can be returned only by doing throwE. If I'm pursuing a functional approach, where errors are truly for exceptional cases, I would rather avoid going through the intermediate step of raising an error just to map to a Left value. Additionally, there are cases where a Left is not the result of an error thrown. For example, validation.

class-validator, and many other validation packages will already return something like a result object. Currently, I would have to do something like this:

      const validationResult = await dto.validate();
      if (validationResult.length > 0) {
        throwE(validationResult);
      }

I would rather simply have a helper left that I can return. It seems like this would also make code flow more consistent, since throwE throws, thereby interrupting other code, which decreases readability.

In JS, there is a performance penalty for throwing anything. This is especially true for Error types, because of stack generation, but is still true for other throwables.

@gigobyte
Copy link
Owner

gigobyte commented Aug 7, 2024

I assume your example code is ran inside the EitherAsync constructor. If you're using async/await the code is supposed to look procedural on purpose. I think you would find using EitherAsync without syntax sugar more elegant (i.e. with map, chain and all of the other utility methods).

Regarding the performance aspect of things, I agree and I would not recommend using purify for performance-critical applications, especially for async code, as the abstractions provided by the library are quite costly. That's not to discourage people from using the library, I mean it in the sense that if you really care about performance I wouldn't recommend neither purify nor JavaScript as a language.

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

No branches or pull requests

2 participants