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

louiscad

04/08/2019, 7:44 PM
A while ago, I made a function to race coroutines (winner cancels all the losers) thanks to your help, and now that I'm planning to add it to a library since it's not in kotlinx.coroutines, I'd like to have your thoughts on its name, and ideas if any. Here's that function, currently named `raceOf`:
Copy code
suspend fun <T> raceOf(vararg racers: suspend CoroutineScope.() -> T): T = coroutineScope {
    select<T> {
        @UseExperimental(ExperimentalCoroutinesApi::class)
        val racersAsyncList = racers.map { async(start = CoroutineStart.UNDISPATCHED, block = it) }
        racersAsyncList.forEach { racer ->
            racer.onAwait { resultOfWinner ->
                racersAsyncList.forEach { deferred -> deferred.cancel() }
                return@onAwait resultOfWinner
            }
        }
    }
}
s

spand

04/08/2019, 7:58 PM
Since I know javascript I like race. I guess the Java equivalent is
invokeAny
but is quite vague. What is it called in golang?
s

simon.vergauwen

04/08/2019, 8:44 PM
In Arrow it's called
raceN
5 Views