withSoftTimeout

suspend fun <T> withSoftTimeout(scope: CoroutineScope, timeoutMs: Long, onTimeout: suspend () -> T, onLateSuccess: suspend (T) -> Unit = {}, onFailure: suspend (Throwable) -> T, block: suspend () -> T): T

Executes a suspending operation with a soft timeout that doesn't cancel the underlying work.

If operation completes within timeoutMs, returns the result immediately. If timeout expires, returns onTimeout while the original work continues running on scope. When the background operation finishes, onLateSuccess is invoked for success, and onFailure for failure.

Parameters

scope

CoroutineScope for background work (must outlive current coroutine)

timeoutMs

Timeout in milliseconds

onTimeout

Fallback executed on timeout

onLateSuccess

Callback invoked when background operation eventually succeeds

onFailure

Callback invoked when the operation fails. Its return value is used when failure happens before the timeout.

block

The suspending operation to execute