onFuture<T> method
Race a Future - handles both success and error cases.
The future is automatically canceled if another branch wins the race. Use this when you need to handle both successful completion and errors.
Parameters:
fut
: Future to racebody
: Function to call with the future's resulttag
: Optional tag for debuggingif_
: Optional guard condition
Example:
await XSelect.run<String>((s) => s
..onFuture(apiCall(), (result) => 'API returned: $result')
..onFuture(fallbackCall(), (result) => 'Fallback: $result')
..onTimeout(Duration(seconds: 5), () => 'timeout')
);
See also:
- onFutureValue - Only handle value
- onFutureError - Only handle error
Implementation
SelectBuilder<R> onFuture<T>(Future<T> fut, FutureOr<R> Function(T) body,
{Object? tag, bool Function()? if_}) {
_branches.add((FutureBranch<T, R>(fut, body, tag: tag), if_));
return this;
}