delayed<T> static method

Future<T> delayed<T>(
  1. Duration duration,
  2. CancellationToken? cancellationToken, [
  3. FutureOr<T> computation()?,
  4. OnCancelCallback? onCancel,
])

Creates a future that runs its computation after a delay, unless cancelled.

This is the cancellable equivalent to Future.delayed().

If the cancellationToken is cancelled during the delay, the computation will not run, and the cancellation exception will be thrown.

If the cancellationToken is cancelled whilst the computation is running, the cancellation exception will be thrown and the result of the computation will be ignored.

If you want to run the computation after the delay regardless of whether or not the cancellationToken has been cancelled, you should use Future.delayed() with the .asCancellable() extension:

await Future.delayed(Duration(seconds: 5), computation)
    .asCancellable(token);

Implementation

static Future<T> delayed<T>(
  Duration duration,
  CancellationToken? cancellationToken, [
  FutureOr<T> Function()? computation,
  OnCancelCallback? onCancel,
]) =>
    _DelayedCancellableFuture(
      duration,
      cancellationToken,
      computation,
      onCancel,
    ).future;