dartz
dartz copied to clipboard
Add flatMapAsync for Either class
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 :)
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?
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>>