arrow-fx / arrow.fx / IORace / racePair
open fun <A, B> racePair(ctx:
CoroutineContext
, ioA:
IOOf
<A>, ioB:
IOOf
<B>):
IO
<
RacePair
<
ForIO
, A, B>>
Race two tasks concurrently within a new IO. Race results in a winner and the other, yet to finish task running in a Fiber.
import arrow.fx.*
import arrow.fx.extensions.fx
import kotlinx.coroutines.Dispatchers
fun main(args: Array<String>) {
//sampleStart
val result = IO.fx {
val racePair = !IO.racePair(Dispatchers.Default, never<Int>(), just("Hello World!"))
racePair.fold(
{ _, _ -> "never cannot win race" },
{ _, winner -> winner }
)
}
//sampleEnd
val r = result.unsafeRunSync()
println("Race winner result is: $r")
}
ctx
- CoroutineContext to execute the source IO on.
ioA
- task to participate in the race
ioB
- task to participate in the race
Return IO either Left with product of the winner’s result ioA and still running task ioB, or Right with product of running task ioA and the winner’s result ioB.
See Also
Do you like Arrow?
✖