Skip to content

Result.TryAsync? #7

@vitidev

Description

@vitidev

I don't see a way to achieve this without adding a method to ResultSharp. Something like...

public static async ValueTask<Result<T, E>> TryAsync<T, E>(Func<Task<T>> fn) where E : Exception
{
    try
    {
        var value = await fn();
        return Result<T, E>.Ok(value);
    }
    catch (E exception)
    {
        return Result<T, E>.Err(exception);
    }
}

public static ValueTask<Result<T, Exception>> TryAsync<T>(Func<Task<T>> fn) =>
    TryAsync<T, Exception>(fn);

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