arrow-fx-coroutines / arrow.fx.coroutines / Promise
interface ~~Promise~~<A>
Deprecated: Use CompletableDeferred
A Promise
is commonly used to provide and receive a value from 2 different threads.
Since Promise
can only be completed once unlike ConcurrentVar, we can consider it a
synchronization primitive.
When made, a Promise is empty, and it remains in this state until it is fulfilled, which can only happen once.
Let’s say we wanted to await a Fiber
, we could complete a Promise latch
to signal it finished.
Awaiting the latch Promise
will now prevent main
from finishing early.
import arrow.fx.coroutines.*
suspend fun main(): Unit {
val await = Promise<Unit>()
ForkConnected {
println("Fiber starting up!")
sleep(3.seconds)
println("Fiber finished!")
await.complete(Unit)
}
await.get() // Suspend until fiber finishes
}
AlreadyFulfilled | object AlreadyFulfilled |
complete | Completes, or fulfills, the promise with the specified value A. Returns Promise.AlreadyFulfilled in Either.Left if the promise is already fulfilled.abstract suspend fun complete(a: A): Either<AlreadyFulfilled, Unit > |
get | Gets the promised value or throws. Use attempt when throwing is not required. Suspends until the promised value is available.abstract suspend fun get(): A |
tryGet | Tries to get the promised value, returning null if promise is not fulfilled yet. Returns A if promise is fulfilled.abstract suspend fun tryGet(): A? |
invoke | suspend operator fun <A> ~~invoke~~(): Promise <A> |
unsafe | fun <A> ~~unsafe~~(): Promise <A> |
Do you like Arrow?
✖