asyncFirst<T> function

Future<T> asyncFirst<T>(
  1. List<T> items,
  2. Future<bool> filter(
    1. T item
    ), {
  3. T orElse()?,
})

Returns a future that completes with the first item in items for which filter returns a future that completes with true. It completes with an item only when its filter future completes with true AND the filter futures for all previous items have completed. If all filter futures complete with false and orElse is absent, completes with an error. If all filter futures complete with false and orElse is present, completes with the return value of orElse.

Example: newKing = asyncFirst(sons, survivesToMaturity);

Implementation

Future<T> asyncFirst<T>(List<T> items, Future<bool> filter(T item),
        {T orElse()?}) =>
    asyncWhere<T>(items, filter)
        .firstWhere((_) => true, orElse: orElse)
        .then((x) => x);