// Created on savesnippets.com · https://savesnippets.com/ggXk5TWYs049pZ import kotlinx.coroutines.* import kotlinx.coroutines.channels.* import kotlinx.coroutines.selects.* fun main() = runBlocking { val fast = Channel() val slow = Channel() launch { delay(100); fast.send("fast") } launch { delay(500); slow.send("slow") } // Wait for whichever channel responds first val first = select { fast.onReceive { it } slow.onReceive { it } } println("got: $first") // fast // With timeout — return null if neither responds in time val maybe = withTimeoutOrNull(50) { select { fast.onReceive { it } slow.onReceive { it } } } println("maybe: $maybe") // null (already consumed) }