Async

Task.WhenAny with take predicate

Not a very common scenario but still it happens, you want to execute a request to several Services, and only one has the correct data. The built in Task.WhenAny only supports to await the first Task complete. That wont do if you want to wait for the first Task to complete that satisfy a specific condition.

Nito.AsyncEx is a helper library for Task programming, it comes with an extension method called OrderByCompletion. It takes a collection of Task<T> and return a new collection that will be ordered on completion status. Using this method its a simple task to create a WhenAny based on a predicate.

public async static Task<TResult> First<TResult>(this IEnumerable<Task<TResult>> source, Predicate<TResult> predicate)
{
    var ordered = source
        .OrderByCompletion();

    foreach (var task in ordered)
    {
        var result = await task;
        if (predicate(result)) return result;
    }

    throw new Exception("Sequence contains no matching element");
}

Used like

var result = await services
   .Select(s => s.GetFooAsync())
   .First(f => f.IsSuccess);
}
Advertisements