final class Promise[F[_], A] extends AnyRef
A purely functional synchronisation primitive.
When created, a Promise
is empty. It can then be completed exactly once, and never be made empty again.
get
on an empty Promise
will block until the Promise
is completed.
get
on a completed Promise
will always immediately return its content.
complete(a)
on an empty Promise
will set it to a
, and notify any and all readers currently blocked on a call to get
.
complete(a)
on a Promise
that's already been completed will not modify its content, and result in a failed F
.
Albeit simple, Promise
can be used in conjunction with Ref to build complex concurrent behaviour and
data structures like queues and semaphores.
Finally, the blocking mentioned above is semantic only, no actual threads are blocked by the implementation.
- Source
- Promise.scala
- Alphabetic
- By Inheritance
- Promise
- AnyRef
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
- def +(other: String): String
- def ->[B](y: B): (Promise[F, A], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
cancellableGet: F[(F[A], F[Unit])]
Like get but returns an
F[Unit]
that can be used to cancel the subscription. -
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
complete(a: A): F[Unit]
If this
Promise
is empty, *synchronously* sets the current value toa
, and notifies any and all readers currently blocked on aget
.If this
Promise
is empty, *synchronously* sets the current value toa
, and notifies any and all readers currently blocked on aget
.Note that the returned action completes after the reference has been successfully set: use
async.fork(r.complete)
if you want asynchronous behaviour.If this
Promise
has already been completed, the returned action immediately fails with a Promise.AlreadyCompletedException. In the uncommon scenario where this behaviour is problematic, you can handle failure explicitly usingattempt
or any otherApplicativeError
/MonadError
combinator on the returned action.Satisfies:
Promise.empty[F, A].flatMap(r => r.complete(a) *> r.get) == a.pure[F]
- def ensuring(cond: (Promise[F, A]) ⇒ Boolean, msg: ⇒ Any): Promise[F, A]
- def ensuring(cond: (Promise[F, A]) ⇒ Boolean): Promise[F, A]
- def ensuring(cond: Boolean, msg: ⇒ Any): Promise[F, A]
- def ensuring(cond: Boolean): Promise[F, A]
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
- def formatted(fmtstr: String): String
-
def
get: F[A]
Obtains the value of the
Promise
, or waits until it has been completed. -
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
timedGet(timeout: FiniteDuration, scheduler: Scheduler): F[Option[A]]
Like get but if the
Promise
has not been completed when the timeout is reached, aNone
is returned. -
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
- def →[B](y: B): (Promise[F, A], B)