waitFor<T> method
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.
reason is optional and is typically not supplied, as a reason is
generated from matcher; if reason is included it is appended to the
reason generated by the matcher.
Implementation
Future<T> waitFor<T>(FutureOr<T> Function() condition,
{Object? matcher,
Duration timeout = defaultTimeout,
Duration interval = defaultInterval,
String? reason}) 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, reason);
}
return value;
} catch (e) {
if (now.isAfter(endTime)) {
rethrow;
} else {
await sleep(interval);
}
}
}
}