race<T> method

Future<T> race<T>(
  1. Future<T> operation
)

Implementation

Future<T> race<T>(Future<T> operation) {
  throwIfCancelled();
  final result = Completer<T>();
  final detach = onCancel(() {
    if (!result.isCompleted) {
      result.completeError(TrustedTimeException('cancelled', _reason));
    }
  });
  operation
      .then(
        (value) {
          if (!result.isCompleted) result.complete(value);
        },
        onError: (Object error, StackTrace stack) {
          if (!result.isCompleted) result.completeError(error, stack);
        },
      )
      .whenComplete(detach);
  return result.future;
}