arrow-fx / arrow.fx / Promise / error

error

abstract fun error(throwable: Throwable): Kind<F, Unit>

Errors the promise with the specified Throwable. Results in an Promise.AlreadyFulfilled within F if the promise is already fulfilled.

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

  promise.flatMap { p ->
    p.error(RuntimeException("Boom"))
  }.attempt().unsafeRunSync() ==
    IO.raiseError<Int>(RuntimeException("Boom")).attempt().unsafeRunSync()

  promise.flatMap { p ->
    p.complete(1).flatMap {
      p.error(RuntimeException("Boom"))
    }
  }.attempt().unsafeRunSync() ==
    IO.raiseError<Int>(Promise.AlreadyFulfilled).attempt().unsafeRunSync()
  //sampleEnd
}

Do you like Arrow?

Arrow Org
<