dartz icon indicating copy to clipboard operation
dartz copied to clipboard

Add flatMapAsync for Either class

Open jvsoaress opened this issue 2 years ago • 2 comments

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 :)

jvsoaress avatar Mar 25 '22 23:03 jvsoaress

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?

spebbe avatar Mar 28 '22 11:03 spebbe

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>>

jvsoaress avatar Apr 17 '22 01:04 jvsoaress