waitFor<T> method

Future<T> waitFor<T>(
  1. FutureOr<T> condition(), {
  2. Object? matcher,
  3. Duration timeout = defaultTimeout,
  4. Duration interval = defaultInterval,
})

Waits until condition evaluates to a value that matches matcher or until timeout time has passed. If condition returns a Future, then uses the value of that Future rather than the value of condition.

If the wait is successful, then the matching return value of condition is returned. Otherwise, if condition throws, then that exception is rethrown. If condition doesn't throw then an expect exception is thrown.

Implementation

Future<T> waitFor<T>(FutureOr<T> Function() condition,
    {Object? matcher,
    Duration timeout = defaultTimeout,
    Duration interval = defaultInterval}) async {
  final mMatcher = matcher == null ? null : m.wrapMatcher(matcher);
  final endTime = now.add(timeout);
  while (true) {
    try {
      final value = await condition();
      if (mMatcher != null) {
        _matcherExpect(value, mMatcher);
      }
      return value;
    } catch (e) {
      if (now.isAfter(endTime)) {
        rethrow;
      } else {
        await sleep(interval);
      }
    }
  }
}