Why is there no DoAsync? #1277
-
I'm just getting my feet wet with LanguageExt and functional programming, so this is probably due to my ignorance, but why is there no DoAsync variant of Do? Seems like there is one for many other operations. I wrote an extension method for it myself, it is not hard to do, so I'm guessing there is probably a reason why it doesn't exist and that I maybe need another approach. What I want to do is get a result from an (async) function, write a log message (this happens in the do, also async), and then pass on the result of the function. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
For no reason other than I don't have infinite time and sometimes things don't get written :D Note, that all *Async stuff will be going away in v5. You will be able to 'lift' async code into the static IO<Unit> waitOneSecond =>
liftIO(async _ =>
{
await Task.Delay(1000);
return unit;
}); Which will mean that running
You can use TryAsync<A> DoAsync(this TryAsync<A> ma, Func<A, Task> todo) =>
ma.MapAsync(async x =>
{
await todo(x).ConfigureAwait(false);
return x;
}); In the case of logging, I see your use-case, and it's not unreasonable. I would probably wrap up your call to TryAsync<A> Log(this TryAsync<A> ma, string msg) =>
ma.Do(async x => await YourLog.Log(x, msg)); Or, create a bespoke monad to do logging - which is ultimately the best approach, but if you're just getting your feet wet, I'd avoid that for now (it's known as 'writer monad' if you're interested). |
Beta Was this translation helpful? Give feedback.
For no reason other than I don't have infinite time and sometimes things don't get written :D
Note, that all *Async stuff will be going away in v5. You will be able to 'lift' async code into the
IO
andEff
monads:Which will mean that running
waitOneSecond
will run the computation concurrently without it having to be an Async variant.Do
is a little bit of a code-smell, so be wary of using it too much. It's a bit of an admission that you're injecting side-effects into your computation, which is usually something to avoid.You can use
MapAsync
…