parSequenceEither

suspend fun <A, B> Iterable<suspend () -> Either<A, B>>.parSequenceEither(): Either<A, List<B>>(source)

Sequences all tasks in parallel on Dispatchers.Default and return the result. If one of the tasks returns Either.Left, then it will short-circuit the operation and cancelling all this running tasks, and returning the first encountered Either.Left.

Cancelling this operation cancels all running tasks.


@JvmName(name = "parSequenceEitherScoped")
suspend fun <A, B> Iterable<suspend CoroutineScope.() -> Either<A, B>>.parSequenceEither(): Either<A, List<B>>(source)
suspend fun <A, B> Iterable<suspend () -> Either<A, B>>.parSequenceEither(ctx: CoroutineContext = EmptyCoroutineContext): Either<A, List<B>>(source)


@JvmName(name = "parSequenceEitherScoped")
suspend fun <A, B> Iterable<suspend CoroutineScope.() -> Either<A, B>>.parSequenceEither(ctx: CoroutineContext = EmptyCoroutineContext): Either<A, List<B>>(source)

Sequences all tasks in parallel on ctx and return the result. If one of the tasks returns Either.Left, then it will short-circuit the operation and cancelling all this running tasks, and returning the first encountered Either.Left.

Coroutine context is inherited from a CoroutineScope, additional context elements can be specified with ctx argument. If the combined context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.Default is used. WARNING If the combined context has a single threaded ContinuationInterceptor, this function will not run in parallel.

Cancelling this operation cancels all running tasks.

import arrow.core.*
import arrow.fx.coroutines.*
import kotlinx.coroutines.Dispatchers

object Error
typealias Task = suspend () -> Either<Throwable, Unit>

suspend fun main(): Unit {
//sampleStart
fun getTask(id: Int): Task =
suspend { Either.catch { println("Working on task $id on ${Thread.currentThread().name}") } }

val res = listOf(1, 2, 3)
.map(::getTask)
.parSequenceEither(Dispatchers.IO)
//sampleEnd
println(res)
}