delay method

  1. @override
Future<void> delay(
  1. Duration duration
)
override

Completes after duration has elapsed on this clock.

A zero or negative duration must complete without delay, but still asynchronously, so that callers cannot accidentally depend on synchronous completion.

Implementation

@override
Future<void> delay(Duration duration) {
  final effective = duration.isNegative ? Duration.zero : duration;
  _requestedDelays.add(effective);

  if (autoAdvance) {
    // Advance on a microtask rather than synchronously, so that code awaiting
    // this delay yields exactly as it would against a real clock. Completing
    // synchronously would hide ordering bugs that only appear in production.
    return Future<void>.microtask(() {
      _now = _now.add(effective);
    });
  }

  if (effective == Duration.zero) return Future<void>.value();

  final pending = _PendingDelay(_now.add(effective), Completer<void>());
  _pending.add(pending);
  return pending.completer.future;
}