arrow-fx / arrow.fx / Promise / tryError

tryError

abstract fun tryError(throwable: Throwable): Kind<F, Boolean>

Tries to error the promise with the specified Throwable. Returns true if the promise already completed or errored, false otherwise.

import arrow.core.Right
import arrow.fx.*
import arrow.fx.extensions.io.async.async
import arrow.fx.extensions.io.monad.flatMap

fun main(args: Array<String>) {
  //sampleStart
  val promise = Promise.uncancellable<ForIO, Int>(IO.async())
  val throwable = RuntimeException("Boom")

  promise.flatMap { p ->
    p.tryError(throwable)
  }.attempt().unsafeRunSync() ==
    IO.raiseError<Int>(throwable).attempt().unsafeRunSync()

  promise.flatMap { p ->
    p.complete(1).flatMap {
      p.tryError(RuntimeException("Boom"))
    }
  }.attempt().unsafeRunSync() == Right(false)
  //sampleEnd
}

Do you like Arrow?

Arrow Org
<