IntermediateCoroutines
withContext IO
Run blocking-style work on Dispatchers.IO.
Dependency required:
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")kotlin
import kotlinx.coroutines.*
suspend fun read(): String = withContext(Dispatchers.IO) {
delay(50)
"data"
}
fun main() = runBlocking {
println(read())
}Output
data
Explanation
withContext switches to the IO dispatcher for the block, then returns the result.