arrow-fx / arrow.fx / Promise / complete

complete

abstract fun complete(a: A): Kind<F, Unit>

Completes, or fulfills, the promise with the specified value A. 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.complete(1).flatMap {
      p.get()
    }
  }.unsafeRunSync() == IO.just(1).unsafeRunSync()

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

Do you like Arrow?

Arrow Org
<