seangwright / typescript-functional-extensions

A TypeScript implementation of the C# library CSharpFunctionalExtensions, including synchronous and asynchronous Maybe and Result monads.
MIT License
33 stars 4 forks source link

How To: Approach branching correctly #21

Open GregOnNet opened 1 year ago

GregOnNet commented 1 year ago

Hey together,

this issue is not a real request, it is a question. I approach various situations where I still need to break out of the Result-Monad. I am wondering if there is a better way to achieve this.

What situation are you talking about?

I try to be as specific as possible. In my project, I need to execute difference processes depending on if an Entity exists or not.

This can be situation can be abstracted to If an initial operation fails, I want to run operation A otherwise I want to run operation B.

What do you mean by breaking out of the monad.

Below you see a code-example. There I check if a resource has been linked to a document. If now I can stop right there and yielding a Result.success, otherwise I need to execute a whole other process. I cannot chain the operations by using map, mapFailure, bind since I would compensate Result.failure of independent operations that have not to do anything with each other. So compensation would be wrong. I am interested in the respected Result.failure if something goes from, and I want to log it afterwards.

In the end, I convert the ResultAsync to a promise. Introduce an if-Statement to do the control-flow.

async link(yardDelivery: YardDelivery): Promise<Result<boolean>> {
    const isAlreadyLinked = await this.yardDocumentSearchService.findOneByYardDelivery(yardDelivery).isSuccess;

    if (isAlreadyLinked) return Result.success(true);

    return await this.linkByTransportationOrderReference(yardDelivery)
      .pipe(bindFailureAsync(() => this.linkByLookingUpYardBookingReference(yardDelivery)))
      .toPromise();
  }

What is bad about the code above.

I am so happy if you can provide some thoughts on this.

Thanks in advance Gregor

GregOnNet commented 1 year ago

https://github.com/gvergnaud/ts-pattern might provide an API that could be adopted for typescript-functional-extensions. 🤔

I think, what I am looking for is: Pattern Matching.

seangwright commented 1 year ago

I'm in the middle of changing jobs atm, so I'm gonna be a little slow on feedback. However, I'll be in the airport some over the next week and I'll use that time to sit down and think about your ideas 👍.

GregOnNet commented 1 year ago

Hi, no worries. I am happy to read your feedback.