What is the difference in using CountAsync or AsyncEnumerable CountAwaitAsync?

Viewed 296

What is the difference in using CountAsync or AsyncEnumerable CountAwaitAsync?

In AsyncEnumerable.cs there is:

public static ValueTask<int> CountAwaitAsync<[Nullable(2)] TSource>(
      [Nullable(1)] this IAsyncEnumerable<TSource> source,
      [Nullable(new byte[] {1, 1, 0})] Func<TSource, ValueTask<bool>> predicate,
      CancellationToken cancellationToken = default (CancellationToken));
  1. Why is there an await in the name?
  2. Why can it only be called with a predicate?

Both make no sense to me, but I am sure there is a sense. I don't get it.

1 Answers

CountAsync is for the case when predicate (Func<TSource, bool>) is synchronous.

CountAwaitAsync is for the case when predicate is an asynchronous lambda. Hence its signature is Func<TSource, ValueTask<bool>>. With default predicate a => true there is no need in asynchronousity, so I think that's why there is no default predicate here.

Update: as per @Sir Rufo, CountAwaitAsync waits for predicate so there is Await in its name.

Related