CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Add OnSuccessAsync, OnFailureAsync

Open ittennull opened this issue 6 years ago • 2 comments

It looks to me that OnSuccess method is crazy overloaded, there are so many overloads that it's often very easy to write code that would call wrong OnSuccess method, f.ex. I had this issue many times - I wanted to call OnSuccess that works with tasks and awaits them but I couldn't without adding 3rd dummy argument for configureAwait, otherwise the compiler would pick a wrong overload that has nothing to do with tasks. Sometimes I had to explicitly specify generic parameters to select the right overload which is ugly. When you have a chain of several OnSuccess methods the compiler would often highlight them all with an error and it's often a challenge to understand which line has the problem because error message is often confusing I think if there was OnSuccessAsync - that would greatly reduce number of such problems. Also it would add clarity - we'd know from the first glance that this method works with tasks. It also follows common guidelines to add Async suffix to async methods.

ittennull avatar Feb 07 '19 18:02 ittennull

I'm still learning how to use this fantastic library (and enjoying the process immensely), but I think I may have run into the same issue. Here's a trivial example:

// the optional bool is required otherwise compiler chokes
await Result.Ok() // Non-Task Result
    .OnSuccess(() => context.SaveChangesAsync(), true);

The above is for this overload: Task<Result<T>> OnSuccess<T>(this Result result, Func<Task<T>> func, bool continueOnCapturedContext = true);

PS: I'm leaning away from OnSuccess()ing my DB calls; this is just to illustrate!

space-alien avatar Feb 10 '19 14:02 space-alien

can you please take a look on my stack overflow question?

ahedreville avatar Jun 27 '19 10:06 ahedreville