withTimeoutResult

suspend fun <T> withTimeoutResult(timeoutMillis: Long, block: suspend CoroutineScope.() -> T): Result<T>(source)

Runs block with a timeout of timeoutMillis and reports a timeout as a failed Result instead of throwing, so it never cancels the parent scope.

  • If this call's own timeout fires, returns Result.failure(TimeoutCancellationException).

  • If block completes in time, returns Result.success.

  • If the caller is cancelled (including by an outer withTimeout firing), the CancellationException is rethrown, never captured.

  • Any other exception thrown by block, including a TimeoutCancellationException escaping from a nested withTimeout, propagates unchanged.

Example:

val profile = withTimeoutResult(2_000) { api.loadProfile() }
.getOrElse { cachedProfile }

Rules: CANCEL_006 (handle TimeoutCancellationException so it does not cancel the scope), CANCEL_007 (clean up resources in finally/NonCancellable on the timeout path). See docs/BEST_PRACTICES_COROUTINES.md.

Requires opting in to ExperimentalStructuredCoroutines.

Return

Result.success with the value of block, or Result.failure holding the TimeoutCancellationException when the timeout elapsed.

Parameters

timeoutMillis

timeout in milliseconds. A value of zero or less fails immediately with a timeout failure, like withTimeout.

block

the work to run; it executes in a child scope with the timeout applied.

See also

Throws

if the caller is cancelled by anything other than this timeout.


Duration overload of withTimeoutResult.

Example:

val profile = withTimeoutResult(2.seconds) { api.loadProfile() }

The duration is converted to whole milliseconds; a positive duration shorter than a millisecond is rounded up to 1 ms, and zero or negative durations time out immediately.

Rules: CANCEL_006, CANCEL_007. See docs/BEST_PRACTICES_COROUTINES.md.

Requires opting in to ExperimentalStructuredCoroutines.

Return

see the milliseconds overload.

Parameters

timeout

timeout as a Duration.

block

the work to run.