CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Potentially missing OnFailure(Func<string, Task>) overload for plain Task<Result>

Open MichalSznajder opened this issue 4 years ago • 1 comments

Thank's for great library! But I have a small problem with the following code on version 2.15.1:

public Task Handler()
{
    return await Result.Success()
        .Bind(() => DeleteReservationAsync(_reservationName))
        .Tap(() => SendEmaiAsync(_email))
        .OnFailure((err) => ReportErrorlAsync(err));
}

I want to delete reservation, send email and if something fails report this. DeleteReservationAsync and SendEmaiAsyncl return plain Task<Result>.

This fails with [CS4014] Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

I added manually following extension:

   public static class Temp
    {
        public static async Task<Result> OnFailure(this Task<Result> resultTask, Func<string, Task> action)
        {
            var result = await resultTask;
            if (result.IsFailure)
                await action(result.Error);
            return result;
        }
    }

and code started to compile.

Is this correct approach? Are we missing yet another overload in the library?

MichalSznajder avatar Apr 23 '21 12:04 MichalSznajder

Yes, looks like it's a missing overload. Though, the action probably shouldn't be restricted to string, the generic T is better here.

vkhorikov avatar Apr 29 '21 03:04 vkhorikov