Permalink
Browse files

TaskSample - TwoItemsSecondMatchesSyncReturns

  • Loading branch information...
bobbymcr committed May 23, 2018
1 parent 00d65a5 commit dc1b2a84fa0096ee62c50aa0597edc27f1f5fc06
@@ -12,9 +12,18 @@ namespace TaskSample.Extensions
public static class FuncTaskExtensions
{
public static Task<T> FirstAsync<T>(this IEnumerable<Func<CancellationToken, Task<T>>> funcs, Predicate<T> pred)
public static async Task<T> FirstAsync<T>(this IEnumerable<Func<CancellationToken, Task<T>>> funcs, Predicate<T> pred)
{
return funcs.First()(CancellationToken.None);
foreach (Func<CancellationToken, Task<T>> func in funcs)
{
T result = await func(CancellationToken.None);
if (pred(result))
{
return result;
}
}
return default(T);
}
}
}
@@ -42,5 +42,20 @@ public void TwoItemsFirstMatchesSyncReturns()
task.IsCompletedSuccessfully.Should().BeTrue();
task.Result.Should().Be("good 1");
}
[Fact]
public void TwoItemsSecondMatchesSyncReturns()
{
IEnumerable<Func<CancellationToken, Task<string>>> funcs = new Func<CancellationToken, Task<string>>[]
{
t => Task.FromResult("not good"),
t => Task.FromResult("good 2")
};
Task<string> task = funcs.FirstAsync(r => r.StartsWith("good", StringComparison.Ordinal));
task.IsCompletedSuccessfully.Should().BeTrue();
task.Result.Should().Be("good 2");
}
}
}

0 comments on commit dc1b2a8

Please sign in to comment.