Replies: 4 comments
-
Hi,
If i run this code, it finished after 12ms (depends on your computers speed), but not the result what you want! How should we resolve this?
Voila! Hope that helps a little bit. P.S. You can write something like this (the following example)... but i never ever recommend that, because that is totally blocking code and you absolutely should avoid this!
Regards. |
Beta Was this translation helpful? Give feedback.
-
@LaszloLueck Thanks for the reply, but unless I misunderstood you, you've totally missed the point of my question. I know about awaiting This question is not about the regular Apologies if I misunderstood you, but the code you posted does not show me how to write a method that returns |
Beta Was this translation helpful? Give feedback.
-
OK, so I realised my mistake. I was thinking along the lines of static TryAsync<int> Divide(int n1, int n2) =>
TryAsync(async () => {
// Simulate some async operation that might fail
await Task.Delay(300);
return n1 / n2;
}); What I was expecting is to be able to do the same for Anyone know if it's possible to do something similar for |
Beta Was this translation helpful? Give feedback.
-
In case it helps anyone, I got an answer on StackOverflow. No there isn't a static static EitherAsync<string, double> Square(double n1, double n2) =>
TryAsync(async () => {
await Task.Delay(300);
return n1 / n2;
})
.ToEither(error => error.Message); Note that the SO answer news up an |
Beta Was this translation helpful? Give feedback.
-
Sorry if this is a dumb question, but I'm confused (as usual).
I am trying to get my head around
EitherAsync
, and am not sure I have it clear. My understanding is that it can be used like a regularEither
, but when you wantasync
code inside. However, I don't think the code in theEitherAsync
-returning method is being awaited.For example, consider the following trivial example...
This returns the correct value, but returns immediately, without the three-second delay I was expecting.
I tried
await
ing the delay, but that required making the methodasync
, which gave a compiler exception "The return type of an async method must be void, Task, Task, a task-like type, IAsyncEnumerable, or IAsyncEnumerator."Anyone able to explain what I'm doing wrong? Thanks
Whilst you're at it, please can you explain why I need the call to
AsTask()
when returning. I picked that up (along with the general idea how to use this monad) from this issue, where Paul Louth included it. I figured if he used it, it must be the right thing to do! However, the code seems to compile and run fine without it, although that could be related to the main question I asked above.Any clarification would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions