https://kotlinlang.org logo
#coroutines
Title
# coroutines
e

efemoney

05/17/2021, 3:09 PM
Say I have two tasks that return the same result. How can I wait for the first of the two tasks to complete?
e

elizarov

05/17/2021, 3:15 PM
Copy code
coroutineScope {
    val t1 = async { task1() }
    val t2 = async { task2() }
    select {
        t1.onAwait { /* t1 is first */ }
        t2.onAwait { /* t2 is first */ }
    }
    t1.cancel()
    t2.cancel() 
}
🙏 6
👍 1
e

efemoney

05/17/2021, 3:18 PM
Awesome, I was just looking at select
4 Views