withTimeout

suspend fun <T> withTimeout(
    timeMillis: Long,
    block: suspend CoroutineScope.() -> T
): T
(source)

Runs a given suspending block of code inside a coroutine with a specified timeout and throws TimeoutCancellationException if timeout was exceeded.

The code that is executing inside the block is cancelled on timeout and the active or next invocation of cancellable suspending function inside the block throws TimeoutCancellationException.

The sibling function that does not throw exception on timeout is withTimeoutOrNull. Note that timeout action can be specified for select invocation with onTimeout clause.

Implementation note: how exactly time is tracked is an implementation detail of CoroutineDispatcher in the context.

Parameters

timeMillis - timeout time in milliseconds.