//arrow-fx-coroutines/arrow.fx.coroutines/parSequenceEither

parSequenceEither

common suspend fun <A, B> Iterable<suspend () -> <[A](par-sequence-either.html), [B](par-sequence-either.html)>>.[parSequenceEither](par-sequence-either.html)(): <[A](par-sequence-either.html), [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[B](par-sequence-either.html)>>

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.

common

@JvmName(name = “parSequenceEitherScoped”)

suspend fun <A, B> Iterable<suspend .() -> <[A](par-sequence-either.html), [B](par-sequence-either.html)>>.[parSequenceEither](par-sequence-either.html)(): <[A](par-sequence-either.html), [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[B](par-sequence-either.html)>>

suspend fun <A, B> Iterable<suspend () -> <[A](par-sequence-either.html), [B](par-sequence-either.html)>>.[parSequenceEither](par-sequence-either.html)(ctx: [CoroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-coroutine-context/index.html) = EmptyCoroutineContext): <[A](par-sequence-either.html), [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[B](par-sequence-either.html)>>

common

@JvmName(name = “parSequenceEitherScoped”)

suspend fun <A, B> Iterable<suspend .() -> <[A](par-sequence-either.html), [B](par-sequence-either.html)>>.[parSequenceEither](par-sequence-either.html)(ctx: [CoroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines/-coroutine-context/index.html) = EmptyCoroutineContext): <[A](par-sequence-either.html), [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[B](par-sequence-either.html)>>

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)
}

Do you like Arrow?

Arrow Org
<