executeCancellableAction<T> function
Future<T>
executeCancellableAction<T>(
- Future<
T> action(), - CancellationToken? cancellationToken,
- Duration? timeout
Executes an asynchronous action that can be cancelled or timed out.
This method wraps the provided action
in a completer and sets up
cancellation and timeout handlers.
Implementation
Future<T> executeCancellableAction<T>(
Future<T> Function() action,
CancellationToken? cancellationToken,
Duration? timeout,
) {
final completer = Completer<T>();
action().then((result) {
if (!completer.isCompleted) {
completer.complete(result);
}
}).catchError((error) {
if (!completer.isCompleted) {
completer.completeError(error);
}
});
handleCancellationAndTimeout<T>(
completer,
cancellationToken: cancellationToken,
timeout: timeout,
operationName: 'Operation',
);
return completer.future;
}