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

mp

02/06/2019, 7:50 PM
Is there a tidy way to wait for any of`n` coroutines to finish (don’t care which one finishes first, just give me the first one)
l

louiscad

02/06/2019, 7:58 PM
You want a race of coroutines with only one winner, and losers cancelled?
m

mp

02/06/2019, 7:58 PM
No, the others keep going — I just want to respond as soon as the first one finishes and possibly add another coroutine to do yet more work
The goal is to keep some
k
tasks going, but I need to look at each as it finishes to know if I should add more work to do
l

louiscad

02/06/2019, 8:43 PM
@mp You can hack into an
actor
I think. You can launch 4 (or
k
) coroutines inside it that then call
receive()
in a
while(isActive)
loop before doing work with the received value.
m

mp

02/06/2019, 10:22 PM
interesting, I’ll look into it
a

ansman

02/06/2019, 10:29 PM
It might be worth checking out the source for
AwaitAll
which does a similar thing: https://github.com/Kotlin/kotlinx.coroutines/blob/master/common/kotlinx-coroutines-core-common/src/Await.kt#L59
👍 1
2 Views