arrow-fx / arrow.fx / Promise / tryGet

tryGet

abstract fun tryGet(): Kind<F, Option<A>>

Try get the promised value, it returns None if promise is not fulfilled yet. Returns Some of A if promise is fulfilled, None otherwise.

import arrow.core.*
import arrow.fx.*
import arrow.fx.typeclasses.*
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.tryGet()
  }.unsafeRunSync() == None

  promise.flatMap { p ->
    p.complete(1).flatMap {
      p.tryGet()
    }
  }.unsafeRunSync() == Some(1)
  //sampleEnd
}

Do you like Arrow?

Arrow Org
<