Skip to content

Use SmartEnumValue.When().Then() with async #564

@stevenvolckaert

Description

@stevenvolckaert

I couldn't find this in the issues or documentation: Is it allowed to switch on SmartEnum values using the .When().Then() pattern, when the Then() action delegate is asynchronous?

For example, in a FastEndpoints endpoint I currently have the following:

public override Task HandleAsync(
    ListAddressRequest request,
    CancellationToken cancellationToken)
{
    ThrowArgumentNullException.If.Null(request);

    var country = Country.FromValue(request.Address.Country);

    country
        .When(Country.Belgium).Then(async () =>
        {
            var listResult = await this.ListBelgiumAddressesAsync(request, cancellationToken);
            await this.SendResultAsync(listResult);
        })
        .Default(async () =>
        {
            var errorResult = Result.Error(new ErrorList
            (
                ErrorMessages: [$"Country '{country.Value}' is not supported."]
            ));
            await this.SendResultAsync(errorResult);
        });

    return Task.CompletedTask;
}

(Country is SmartEnum<Country, string> where each value is the country code.)

Both action delegates passed to Then() and Default() are async, and the code compiles. But will this execute correctly?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions