onTimeout method

SelectBuilder<R> onTimeout(
  1. Duration d,
  2. FutureOr<R> body()
)

Add a global timeout to the entire selection.

Unlike onDelay, this applies to the entire selection. If no other branch completes within the timeout duration, the timeout branch will fire and all other operations will be canceled.

Parameters:

  • d: Duration before timeout
  • body: Function to call on timeout

Example:

// Network request with fallback
final result = await XSelect.run<ApiResponse>((s) => s
  ..onFuture(primaryApi(), (response) => response)
  ..onFuture(secondaryApi(), (response) => response)
  ..onFuture(cacheApi(), (response) => response)
  ..onTimeout(Duration(seconds: 30), () => ApiResponse.timeout())
);

Implementation

SelectBuilder<R> onTimeout(Duration d, FutureOr<R> Function() body) {
  _branches.add((TimerBranch<R>.once(d, body, tag: 'timeout'), null));
  return this;
}