spebbe / dartz

Functional programming in Dart
MIT License
749 stars 60 forks source link

Add flatMapAsync for Either class #103

Open jvsoaress opened 2 years ago

jvsoaress commented 2 years ago

Sometimes I want to run asynchronous operations inside a .flatMap call. For that I'd suggest adding a .flatMapAsync() method to be able to call await inside the callback function.

I already implemented it in a project I'm working on, so I could open a pr for it as well :)

spebbe commented 2 years ago

Hi @docputs!

Thanks for the suggestion – I agree that something like this would be useful! Currently, you can use anEitherValue.traverseFuture(asyncComputationEvaluatingToEither).flatMap(id) for this. Adding a traverseFutureM method to Either would get rid of the .flatMap(id) and would then behave like your proposed flatMapAsync. It would also preserve existing naming conventions (like in https://github.com/spebbe/dartz/blob/master/lib/src/ilist.dart#L315) and make it somewhat more explicit that a traversal and a monadic join is happening, rather than just a monadic bind. Does that sound like a reasonable solution to you?

jvsoaress commented 2 years ago

I didnt get the part where you wrote asyncComputationEvaluatingToEither, because currently the .traverseFuture accepts a callback that returns Future<R2>. In my use case, I need to return a Future<Either<L2, R2>>