completedWithinDeadline method

Future<bool> completedWithinDeadline({
  1. required Duration deadline,
  2. required Future<bool> future(),
  3. Duration tick = const Duration(milliseconds: 100),
})

Implementation

Future<bool> completedWithinDeadline({
  required Duration deadline,
  required Future<bool> Function() future,
  Duration tick = const Duration(milliseconds: 100),
}) async {
  final Stopwatch stopwatch = Stopwatch()..start();
  bool completed = false;
  await Future.doWhile(() async {
    completed = await future();
    if (completed ||
        stopwatch.elapsedMilliseconds > deadline.inMilliseconds) {
      return false;
    } else {
      await Future.delayed(tick);
      return true;
    }
  });

  return completed;
}