from<T> static method

Future<T> from<T>(
  1. FutureOr<T> computation(),
  2. CancellationToken? cancellationToken, {
  3. OnCancelCallback? onCancel,
})

Creates a cancellable future containing the result of calling computation asynchronously with Timer.run.

This is the cancellable equivalent to Future().

If the cancellationToken has already been cancelled when this is called, the computation will not run.

Implementation

static Future<T> from<T>(
  FutureOr<T> Function() computation,
  CancellationToken? cancellationToken, {
  OnCancelCallback? onCancel,
}) {
  if (cancellationToken?.isCancelled ?? false) {
    return Future<T>.error(cancellationToken!.exception!);
  } else {
    return CancellableFuture<T>._(
      Future(computation),
      cancellationToken,
      onCancel: onCancel,
    ).future;
  }
}