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

andTee() / andThrough() to handle side-effects a bit easier (from issue #445) #467

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

untidy-hair
Copy link
Contributor

@untidy-hair untidy-hair commented Apr 3, 2023

closes #445

Hi @supermacro!
I opened an issue with a suggestion about 2.5 months ago (Jan 17) but I think you have been very busy and I thought maybe a real code would save your time :)

I got a feedback comment in the original issue thread and I ended up having these two methods.

  1. andTee()
andTee(f: (t: T) => unknown): ResultAsync<T, E>
// Usage: validate() below returns ResultAsync
validate(userInfoInput).andThen(buildUser).andTee(logEvent).andThen(notifyUserRegistration)....
  1. andThrough()
andThrough<F>(f: (t: T) => ResultAsync<unknown, F>): ResultAsync<T, E | F>
// Usage: validate() below returns ResultAsync
validate(userInfoInput).andThen(buildUser).andThrough(persistUser).andThen(notifyUserRegistration)....

Both return the original input okAsync<T> upon success, but in case of error, andTee() ignores the error whereas andThrough() returns the new error errAsync<F>. The former is suitable for such as logging when you don't want to stop the main logic upon trivial side-effect failure and the latter is suitable when you want to stop the main logic upon a critical side-effect failure such as DB insertion error.

Origianlly I was going to use the name andTee for no.2 above that passes error value in case of an error but considering the original meaning of "tee", even the error should not come back to the main "pipe" so I used the name for no.1 above instead. (But as I wrote in the original issue, there could be better names.)

And was also written in the original thread, this can be done with the current APIs but these two new methods will make it possible to write and understand each step of the logics more vividly (IMO) and I personally have cases I want to rewrite code with andThrough() for DB persistence as I explained here :)

Thanks in advance!

@brndt
Copy link

brndt commented May 29, 2023

@supermacro can you take a look on this please? It's quite useful feature to avoid boilerplate

@brndt
Copy link

brndt commented Sep 12, 2023

@supermacro bump :)

@supermacro
Copy link
Owner

#496

@untidy-hair
Copy link
Contributor Author

untidy-hair commented Dec 6, 2023

Hi @supermacro , have you had a chance to look at this PR? I'd appreciate your review! Thank you!

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.

Add andTee() implementation
4 participants