withTimeoutOrDefault

suspend fun <T> withTimeoutOrDefault(timeout: Duration, default: T, block: suspend CoroutineScope.() -> T): T

Runs block with the given timeout and returns default if the timeout elapses first.

A non-positive timeout returns default without running block. Failures thrown by block propagate unchanged.

Cancellation: on timeout, block is cancelled and default is returned. Cancellation of the calling coroutine is rethrown and is never converted into default.

Parameters

timeout

how long block may run before it is cancelled.

default

the value returned when the timeout elapses.