Class ResultTaskExtensions

Namespace
REslava.Result.Extensions
Assembly
REslava.Result.dll

Extension methods for working with Task<Result<T>>.

public static class ResultTaskExtensions
Inheritance
ResultTaskExtensions
Inherited Members

Methods

MapAsync<T, U>(Task<Result<T>>, Func<T, Task<U>>, CancellationToken)

Asynchronously maps the value inside a Task<Result<T>> to a new value.

public static Task<Result<U>> MapAsync<T, U>(this Task<Result<T>> resultTask, Func<T, Task<U>> mapper, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>

The task containing the result to map.

mapper Func<T, Task<U>>

The async function to transform the value.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task<Result<U>>

A task containing the mapped result.

Type Parameters

T

The source value type.

U

The target value type.

Examples

var result = await GetUserAsync(userId)
    .MapAsync(async user => await GetUserProfileAsync(user.Id));

MapAsync<T, U>(Task<Result<T>>, Func<T, U>, CancellationToken)

Asynchronously maps the value inside a Task<Result<T>> to a new value of type U.

public static Task<Result<U>> MapAsync<T, U>(this Task<Result<T>> resultTask, Func<T, U> mapper, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>

The task containing the result to map.

mapper Func<T, U>

The function to transform the value.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task<Result<U>>

A task containing the mapped result.

Type Parameters

T

The source value type.

U

The target value type.

Examples

var result = await GetUserAsync(userId)
    .MapAsync(user => user.Name);

WithSuccessAsync<T>(Task<Result<T>>, ISuccess, CancellationToken)

Asynchronously adds a success reason to a Task<Result<T>>.

public static Task<Result<T>> WithSuccessAsync<T>(this Task<Result<T>> resultTask, ISuccess success, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>

The task containing the result to add success to.

success ISuccess

The success reason to add.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task<Result<T>>

A task containing the result with the added success reason.

Type Parameters

T

The value type.

Examples

var success = new Success("User created");
var result = await CreateUserAsync(user)
    .WithSuccessAsync(success);

WithSuccessAsync<T>(Task<Result<T>>, string, CancellationToken)

Asynchronously adds a success reason to a Task<Result<T>>.

public static Task<Result<T>> WithSuccessAsync<T>(this Task<Result<T>> resultTask, string message, CancellationToken cancellationToken = default)

Parameters

resultTask Task<Result<T>>

The task containing the result to add success to.

message string

The success message to add.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task<Result<T>>

A task containing the result with the added success reason.

Type Parameters

T

The value type.

Examples

var result = await SaveUserAsync(user)
    .WithSuccessAsync("User saved successfully");