CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Calling an async method OnSuccess

Open BennieCopeland opened this issue 8 years ago • 3 comments

Is it possible to call async methods, or do I need to use Result or Wait()?

BennieCopeland avatar Jan 31 '18 20:01 BennieCopeland

It should work with async operations too. Check out examples here: https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions.Examples/ResultExtensions/AsyncUsageExamples.cs

vkhorikov avatar Feb 01 '18 20:02 vkhorikov

I've be playing around with it now, and I think I've found an issue, or I'm not using it right. My code looks like the following:

return Result.Combine(name, address)
    .OnSuccess(async () =>
    {
        try
        {
            return Result.Ok(await eventstore.GetAsync<User>(message.UserId));
        }
        catch (AggregateNotFoundException)
        {
            return Result.Fail<User>("Aggregate not found");
        }
    })
    .OnSuccess(user => user.UpdateAddress(address.Value))
    .OnSuccess(async user =>
    {
        var saveResult = await eventStore.SaveAsync(user);
        return Result.Ok(new CommandResult2(userId.ToString(), saveResult.StreamPosition, saveResult.AggregateVersion));
    })

The first OnSuccess is using public static Result<T> OnSuccess<T>(this Result result, Func<T> func); instead of public static Task<Result<T>> OnSuccess<T>(this Result result, Func<Task<Result<T>>> func, bool continueOnCapturedContext = false);

BennieCopeland avatar Feb 09 '18 18:02 BennieCopeland

I've found that if I change the first OnSuccess to OnSuccess<User> it works.

BennieCopeland avatar Feb 18 '18 14:02 BennieCopeland