arrow-fx / arrow.fx.extensions / IOConcurrent / raceN
open fun <A, B>
CoroutineContext
.raceN(fa: Kind<
ForIO
, A>, fb: Kind<
ForIO
, B>):
IO
<
Race2
<A, B>>
Race two tasks concurrently within a new F on this@raceN. At the end of the race it automatically cancels the loser.
import arrow.Kind
import arrow.fx.*
import arrow.fx.typeclasses.Concurrent
import kotlinx.coroutines.Dispatchers
import arrow.fx.extensions.io.concurrent.concurrent
fun main(args: Array<String>) {
fun <F> Concurrent<F>.example(): Kind<F, String> {
val never: Kind<F, Int> = cancellable { effect { println("Never got cancelled for losing.") } }
//sampleStart
val result = fx.concurrent {
val eitherGetOrUnit = !Dispatchers.Default.raceN(never, just(5))
eitherGetOrUnit.fold(
{ "Never always loses race" },
{ i -> "Race was won with $i" }
)
}
//sampleEnd
return result
}
IO.concurrent().example().fix().unsafeRunSync().let(::println)
}
this
- @raceN CoroutineContext to execute the source F on.
fa
- task to participate in the race
fb
- task to participate in the race
Return F either Left if fa won the race, or Right if fb won the race.
See Also
open fun <A, B, C>
CoroutineContext
.raceN(fa: Kind<
ForIO
, A>, fb: Kind<
ForIO
, B>, fc: Kind<
ForIO
, C>):
IO
<
Race3
<A, B, C>>
See Also
Do you like Arrow?
✖