withTimeout
が使える。
import kotlinx.coroutines.* suspend fun fetchText(): String { delay(2000) return "text" } fun main() { runBlocking { runCatching { withTimeout(1000) { fetchText() } }.onSuccess { println("$it") }.onFailure { println("$it") } } }
withTimeout
が使える。
import kotlinx.coroutines.* suspend fun fetchText(): String { delay(2000) return "text" } fun main() { runBlocking { runCatching { withTimeout(1000) { fetchText() } }.onSuccess { println("$it") }.onFailure { println("$it") } } }