withTimeoutOrNull

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

Runs a given suspending block of code inside a coroutine with a specified timeout and returns null if this 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 a TimeoutCancellationException.

The sibling function that throws an exception on timeout is withTimeout. Note that the timeout action can be specified for a select invocation with onTimeout clause.

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

Parameters

timeMillis - timeout time in milliseconds.