Class/Object

com.twitter.util

Promise

Related Docs: object Promise | package util

Permalink

class Promise[A] extends Future[A] with Responder[A] with Updatable[Try[A]]

A writeable com.twitter.util.Future that supports merging. Callbacks (responders) of Promises are scheduled with com.twitter.concurrent.Scheduler.

Implementation details

A Promise is in one of six states: Waiting, Interruptible, Interrupted, Transforming, Done and Linked where Interruptible, Interrupted, and Transforming are variants of Waiting to deal with future interrupts. Promises are concurrency-safe, using lock-free operations throughout. Callback dispatch is scheduled with Scheduler.

Waiters (i.e., continuations) are stored in a Promise.WaitQueue and executed in the LIFO order.

Promise.become merges two promises: they are declared equivalent. become merges the states of the two promises, and links one to the other. Thus promises support the analog to tail-call elimination: no space leak is incurred from flatMap in the tail position since intermediate promises are merged into the root promise.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Promise
  2. Updatable
  3. Responder
  4. Future
  5. Awaitable
  6. AnyRef
  7. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Promise(result: Try[A])

    Permalink
  2. new Promise(handleInterrupt: PartialFunction[Throwable, Unit])

    Permalink
  3. new Promise()

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def addEventListener(listener: FutureEventListener[_ >: A]): Future[A]

    Permalink

    Register a FutureEventListener to be invoked when the computation completes.

    Register a FutureEventListener to be invoked when the computation completes. This method is typically used by Java programs because it avoids the use of small Function objects.

    Definition Classes
    Future
    Note

    this should be used for side-effects

    See also

    transformedBy for a Java friendly way to produce a new Future from the result of a computation.

    respond for a Scala API.

  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def become(other: Future[A]): Unit

    Permalink

    Become the other promise.

    Become the other promise. become declares an equivalence relation: this and other are the same.

    By becoming other, its waitlists are now merged into this's, and this becomes canonical. The same is true of interrupt handlers: other's interrupt handler is overwritten with the handlers installed for this.

    Note: Using become and setInterruptHandler on the same promise is not recommended. Consider the following, which demonstrates unexpected behavior related to this usage.

    val a, b = new Promise[Unit]
    a.setInterruptHandler { case _ => println("A") }
    b.become(a)
    b.setInterruptHandler { case _ => println("B") }
    a.raise(new Exception)

    This prints "B", the action in the interrupt handler for b, which is unexpected because we raised on a. In this case and others, using com.twitter.util.Future.proxyTo may be more appropriate.

    Note that this must be unsatisfied at the time of the call, and not race with any other setters. become is a form of satisfying the promise.

    This has the combined effect of compressing the other into this, effectively providing a form of tail-call elimination when used in recursion constructs. transform (and thus any other combinator) use this to compress Futures, freeing them from space leaks when used with recursive constructions.

    Note: do not use become with cyclic graphs of futures: the behavior of racing a.become(b) with b.become(a) is undefined (where a and b may resolve as such transitively).

    See also

    com.twitter.util.Future.proxyTo

  7. def before[B](f: ⇒ Future[B])(implicit ev: <:<[Promise.this.type, Future[Unit]]): Future[B]

    Permalink

    Sequentially compose this with f.

    Sequentially compose this with f. This is as flatMap, but discards the result of this. Note that this applies only Unit-valued Futures — i.e. side-effects.

    Definition Classes
    Future
  8. def by(timer: Timer, when: Time, exc: ⇒ Throwable): Future[A]

    Permalink

    Returns a new Future that fails if it is not satisfied before the given time.

    Returns a new Future that fails if it is not satisfied before the given time.

    Note: On timeout, the underlying future is not interrupted.

    Note: Interrupting a returned future would not prevent it from being satisfied with a given exception (when the time comes).

    timer

    to run timeout on.

    when

    indicates when to stop waiting for the result to be available.

    exc

    exception to throw.

    Definition Classes
    Future
  9. def by(timer: Timer, when: Time): Future[A]

    Permalink

    Returns a new Future that fails if it is not satisfied before the given time.

    Returns a new Future that fails if it is not satisfied before the given time.

    Note: On timeout, the underlying future is not interrupted.

    Definition Classes
    Future
  10. def by(when: Time)(implicit timer: Timer): Future[A]

    Permalink

    Returns a new Future that fails if it is not satisfied before the given time.

    Returns a new Future that fails if it is not satisfied before the given time.

    Same as the other by, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is not interrupted.

    Definition Classes
    Future
  11. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  12. final def compress(): Promise[A]

    Permalink

    Should only be called when this Promise has already been fulfilled or it is becoming another Future via become.

    Should only be called when this Promise has already been fulfilled or it is becoming another Future via become.

    Attributes
    protected
  13. final def continue(k: K[A]): Unit

    Permalink
    Attributes
    protected
    Definition Classes
    PromiseResponder
    Annotations
    @tailrec()
  14. final def continueAll(wq: WaitQueue[A]): Unit

    Permalink
    Attributes
    protected
    Definition Classes
    Responder
  15. def delayed(howlong: Duration)(implicit timer: Timer): Future[A]

    Permalink

    Delay the completion of this Future for at least howlong from now.

    Delay the completion of this Future for at least howlong from now.

    Note: Interrupting a returned future would not prevent it from becoming this future (when the time comes).

    Definition Classes
    Future
  16. final def detach(k: K[A]): Boolean

    Permalink
    Attributes
    protected
    Annotations
    @tailrec()
  17. def ensure(f: ⇒ Unit): Future[A]

    Permalink

    Invoked regardless of whether the computation completed successfully or unsuccessfully.

    Invoked regardless of whether the computation completed successfully or unsuccessfully. Implemented in terms of respond so that subclasses control evaluation order. Returns a chained Future.

    The returned Future will be satisfied when this, the original future, is done.

    f

    the side-effect to apply when the computation completes.

    Definition Classes
    Future
    Note

    this should be used for side-effects.

    See also

    respond if you need the result of the computation for usage in the side-effect.

  18. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  19. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  20. def filter(p: (A) ⇒ Boolean): Future[A]

    Permalink
    Definition Classes
    Future
  21. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  22. def flatMap[B](f: (A) ⇒ Future[B]): Future[B]

    Permalink

    If this, the original future, succeeds, run f on the result.

    If this, the original future, succeeds, run f on the result.

    The returned result is a Future that is satisfied when the original future and the callback, f, are done. If the original future fails, this one will also fail, without executing f and preserving the failed computation of this.

    Definition Classes
    Future
    See also

    map

  23. def flatten[B](implicit ev: <:<[A, Future[B]]): Future[B]

    Permalink

    Converts a Future[Future[B]] into a Future[B].

    Converts a Future[Future[B]] into a Future[B].

    Definition Classes
    Future
  24. def foreach(k: (A) ⇒ Unit): Future[A]

    Permalink

    Invoke the callback only if the Future returns successfully.

    Invoke the callback only if the Future returns successfully. Useful for Scala for comprehensions. Use onSuccess instead of this method for more readable code.

    Definition Classes
    Future
    See also

    onSuccess

  25. final def forwardInterruptsTo(other: Future[_]): Unit

    Permalink

    Forward interrupts to another future.

    Forward interrupts to another future. If the other future is fulfilled, this is a no-op. Calling this multiple times is not recommended as the resulting state may not be as expected.

    other

    the Future to which interrupts are forwarded.

    Annotations
    @tailrec()
  26. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  27. def handle[B >: A](rescueException: PartialFunction[Throwable, B]): Future[B]

    Permalink

    If this, the original future, results in an exceptional computation, rescueException may convert the failure into a new result.

    If this, the original future, results in an exceptional computation, rescueException may convert the failure into a new result.

    The returned result is a Future that is satisfied when the original future and the callback, rescueException, are done.

    This is the equivalent of map for failed computations.

    Definition Classes
    Future
    See also

    rescue

  28. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  29. def interruptible(): Future[A]

    Permalink

    Makes a derivative Future which will be satisfied with the result of the parent.

    Makes a derivative Future which will be satisfied with the result of the parent. However, if it's interrupted, it will detach from the parent Future, satisfy itself with the exception raised to it, and won't propagate the interruption back to the parent Future.

    This is useful for when a Future is shared between many contexts, where it may not be useful to discard the underlying computation if just one context is no longer interested in the result. In particular, this is different from Future.masked in that it will prevent memory leaks if the parent Future will never be satisfied, because closures that are attached to this derivative Future will not be held onto by the killer Future.

    Definition Classes
    Future
  30. def isDefined: Boolean

    Permalink

    Is the result of the Future available yet?

    Is the result of the Future available yet?

    Definition Classes
    PromiseFuture
  31. def isDone(implicit ev: <:<[Promise.this.type, Future[Unit]]): Boolean

    Permalink

    Checks whether a Unit-typed Future is done.

    Checks whether a Unit-typed Future is done. By convention, futures of type Future[Unit] are used for signalling.

    Definition Classes
    Future
  32. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  33. def isInterrupted: Option[Throwable]

    Permalink

    Returns this promise's interrupt if it is interrupted.

  34. def isReady(implicit permit: CanAwait): Boolean

    Permalink

    Is this Awaitable ready? In other words: would calling Awaitable.ready block?

    Is this Awaitable ready? In other words: would calling Awaitable.ready block?

    Definition Classes
    PromiseAwaitable
  35. def join[B](other: Future[B]): Future[(A, B)]

    Permalink

    Joins this future with a given other future into a Future[(A, B)] (future of a Tuple2).

    Joins this future with a given other future into a Future[(A, B)] (future of a Tuple2). If this or other future fails, the returned Future is immediately satisfied by that failure.

    Definition Classes
    Future
  36. def joinWith[B, C](other: Future[B])(fn: (A, B) ⇒ C): Future[C]

    Permalink

    Joins this future with a given other future and applies fn to its result.

    Joins this future with a given other future and applies fn to its result. If this or other future fails, the returned Future is immediately satisfied by that failure.

    Before (using join):

    val ab = a.join(b).map { case (a, b) => Foo(a, b) }

    After (using joinWith):

    val ab = a.joinWith(b)(Foo.apply)
    Definition Classes
    Future
  37. def liftToTry: Future[Try[A]]

    Permalink

    Returns the result of the computation as a Future[Try[A]].

    Returns the result of the computation as a Future[Try[A]].

    Definition Classes
    Future
  38. final def link(target: Promise[A]): Unit

    Permalink
    Attributes
    protected
    Annotations
    @tailrec()
  39. def lowerFromTry[B](implicit ev: <:<[A, Try[B]]): Future[B]

    Permalink

    Lowers a Future[Try[T]] into a Future[T].

    Lowers a Future[Try[T]] into a Future[T].

    Definition Classes
    Future
  40. def map[B](f: (A) ⇒ B): Future[B]

    Permalink

    If this, the original future, succeeds, run f on the result.

    If this, the original future, succeeds, run f on the result.

    The returned result is a Future that is satisfied when the original future and the callback, f, are done. If the original future fails, this one will also fail, without executing f and preserving the failed computation of this.

    Definition Classes
    Future
    See also

    onSuccess for side-effecting chained computations.

    flatMap for computations that return Futures.

  41. def mask(pred: PartialFunction[Throwable, Boolean]): Future[A]

    Permalink

    Returns an identical future except that it ignores interrupts which match a predicate

    Returns an identical future except that it ignores interrupts which match a predicate

    Definition Classes
    Future
  42. def masked: Future[A]

    Permalink

    Returns an identical future that ignores all interrupts

    Returns an identical future that ignores all interrupts

    Definition Classes
    Future
  43. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  44. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  45. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  46. def onFailure(fn: (Throwable) ⇒ Unit): Future[A]

    Permalink

    Invoke the function on the error, if the computation was unsuccessful.

    Invoke the function on the error, if the computation was unsuccessful. Returns a chained Future as in respond.

    returns

    chained Future

    Definition Classes
    Future
    Note

    if fn is a PartialFunction and the input is not defined for a given Throwable, the resulting MatchError will propagate to the current Monitor. This will happen if you use a construct such as future.onFailure { case NonFatal(e) => ... } when the Throwable is "fatal".

    ,

    this should be used for side-effects.

    See also

    handle and rescue to produce a new Future from the result of the computation.

  47. def onSuccess(f: (A) ⇒ Unit): Future[A]

    Permalink

    Invoke the function on the result, if the computation was successful.

    Invoke the function on the result, if the computation was successful. Returns a chained Future as in respond.

    returns

    chained Future

    Definition Classes
    Future
    Note

    this should be used for side-effects.

    See also

    flatMap and map to produce a new Future from the result of the computation.

  48. def or[U >: A](other: Future[U]): Future[U]

    Permalink

    A synonym for select: Choose the first Future to be satisfied.

    A synonym for select: Choose the first Future to be satisfied.

    Definition Classes
    Future
  49. def poll: Option[Try[A]]

    Permalink

    Polls for an available result.

    Polls for an available result. If the Future has been satisfied, returns Some(result), otherwise None.

    Definition Classes
    PromiseFuture
  50. def proxyTo[B >: A](other: Promise[B]): Unit

    Permalink

    Send updates from this Future to the other.

    Send updates from this Future to the other. other must not yet be satisfied at the time of the call. After this call, nobody else should satisfy other.

    Definition Classes
    Future
    Note

    using proxyTo will mask interrupts to this future, and it's the user's responsibility to set an interrupt handler on other to raise on f. In some cases, using com.twitter.util.Promise.become may be more appropriate.

    See also

    com.twitter.util.Promise.become

  51. final def raise(intr: Throwable): Unit

    Permalink

    Raise the given throwable as an interrupt.

    Raise the given throwable as an interrupt. Interrupts are one-shot and latest-interrupt wins. That is, the last interrupt to have been raised is delivered exactly once to the Promise responsible for making progress on the future (multiple such promises may be involved in flatMap chains).

    Raising an interrupt does not alter the externally observable state of the Future. They are used to signal to the producer of the future's value that the result is no longer desired (for whatever reason given in the passed Throwable).

    Definition Classes
    PromiseFuture
    Annotations
    @tailrec()
  52. def raiseWithin(timer: Timer, timeout: Duration, exc: Throwable): Future[A]

    Permalink

    Returns a new Future that fails if this Future does not return in time.

    Returns a new Future that fails if this Future does not return in time.

    Note: On timeout, the underlying future is interrupted.

    Definition Classes
    Future
  53. def raiseWithin(timeout: Duration, exc: Throwable)(implicit timer: Timer): Future[A]

    Permalink

    Returns a new Future that fails if this Future does not return in time.

    Returns a new Future that fails if this Future does not return in time.

    Same as the other raiseWithin, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is interrupted.

    Definition Classes
    Future
  54. def raiseWithin(timeout: Duration)(implicit timer: Timer): Future[A]

    Permalink

    Returns a new Future that fails if this Future does not return in time.

    Returns a new Future that fails if this Future does not return in time.

    Same as the other raiseWithin, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is interrupted.

    Definition Classes
    Future
  55. def ready(timeout: Duration)(implicit permit: CanAwait): Promise.this.type

    Permalink

    Support for Await.ready.

    Support for Await.ready. The use of the implicit permit is an access control mechanism: only Await.ready may call this method.

    Definition Classes
    PromiseAwaitable
    Annotations
    @throws( classOf[TimeoutException] ) @throws( classOf[InterruptedException] )
  56. def rescue[B >: A](rescueException: PartialFunction[Throwable, Future[B]]): Future[B]

    Permalink

    If this, the original future, results in an exceptional computation, rescueException may convert the failure into a new result.

    If this, the original future, results in an exceptional computation, rescueException may convert the failure into a new result.

    The returned result is a Future that is satisfied when the original future and the callback, rescueException, are done.

    This is the equivalent of flatMap for failed computations.

    Definition Classes
    Future
    See also

    handle

  57. def respond(k: (Try[A]) ⇒ Unit): Future[A]

    Permalink

    Note: exceptions in responds are monitored.

    Note: exceptions in responds are monitored. That is, if the computation k throws a raw (ie. not encoded in a Future) exception, it is handled by the current monitor, see Monitor for details.

    Definition Classes
    Responder
  58. def result(timeout: Duration)(implicit permit: CanAwait): A

    Permalink

    Support for Await.result.

    Support for Await.result. The use of the implicit permit is an access control mechanism: only Await.result may call this method.

    Definition Classes
    PromiseAwaitable
    Annotations
    @throws( classOf[Exception] )
  59. def select[U >: A](other: Future[U]): Future[U]

    Permalink

    Choose the first Future to be satisfied.

    Choose the first Future to be satisfied.

    other

    another Future

    returns

    a new Future whose result is that of the first of this and other to return

    Definition Classes
    Future
  60. def setDone()(implicit ev: <:<[Promise.this.type, Promise[Unit]]): Boolean

    Permalink

    Sets a Unit-typed future.

    Sets a Unit-typed future. By convention, futures of type Future[Unit] are used for signalling.

  61. def setException(throwable: Throwable): Unit

    Permalink

    Populate the Promise with the given exception.

    Populate the Promise with the given exception.

    Exceptions thrown

    ImmutableResult if the Promise is already populated

  62. final def setInterruptHandler(f: PartialFunction[Throwable, Unit]): Unit

    Permalink

    (Re)sets the interrupt handler.

    (Re)sets the interrupt handler. There is only one active interrupt handler.

    f

    the new interrupt handler

    Annotations
    @tailrec()
  63. def setValue(result: A): Unit

    Permalink

    Populate the Promise with the given result.

    Populate the Promise with the given result.

    Exceptions thrown

    ImmutableResult if the Promise is already populated

  64. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  65. def toJavaFuture: java.util.concurrent.Future[_ <: A]

    Permalink

    Convert a Twitter Future to a Java native Future.

    Convert a Twitter Future to a Java native Future. This should match the semantics of a Java Future as closely as possible to avoid issues with the way another API might use them. See:

    http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html#cancel(boolean)

    Definition Classes
    Future
  66. def toOffer: Offer[Try[A]]

    Permalink

    An Offer for this future.

    An Offer for this future.

    The offer is activated when the future is satisfied.

    Definition Classes
    Future
  67. def toString(): String

    Permalink
    Definition Classes
    Promise → AnyRef → Any
  68. def transform[B](f: (Try[A]) ⇒ Future[B]): Future[B]

    Permalink
    Definition Classes
    Responder
  69. def transformedBy[B](transformer: FutureTransformer[A, B]): Future[B]

    Permalink

    Transform the Future[A] into a Future[B] using the FutureTransformer.

    Transform the Future[A] into a Future[B] using the FutureTransformer. The FutureTransformer handles both success (Return) and failure (Throw) values by implementing map/flatMap and handle/rescue. This method is typically used by Java programs because it avoids the use of small Function objects.

    Definition Classes
    Future
    Note

    The FutureTransformer must implement either flatMap or map and may optionally implement handle. Failing to implement a method will result in a run-time error (AbstractMethodError).

    See also

    addEventListener for a Java friendly way to perform side-effects.

    transform for a Scala API.

  70. def unit: Future[Unit]

    Permalink

    Convert this Future[A] to a Future[Unit] by discarding the result.

    Convert this Future[A] to a Future[Unit] by discarding the result.

    Definition Classes
    Future
    Note

    failed futures will remain as is.

  71. def update(result: Try[A]): Unit

    Permalink

    Populate the Promise with the given Try.

    Populate the Promise with the given Try. The try can either be a value or an exception. setValue and setException are generally more readable methods to use.

    Definition Classes
    PromiseUpdatable
    Exceptions thrown

    ImmutableResult if the Promise is already populated

  72. final def updateIfEmpty(result: Try[A]): Boolean

    Permalink

    Populate the Promise with the given Try.

    Populate the Promise with the given Try. The Try can either be a value or an exception. setValue and setException are generally more readable methods to use.

    returns

    true only if the result is updated, false if it was already set.

    Annotations
    @tailrec()
    Note

    Invoking updateIfEmpty without checking the boolean result is almost never the right approach. Doing so is generally unsafe unless race conditions are acceptable.

  73. def voided: Future[Void]

    Permalink

    Convert this Future[A] to a Future[Void] by discarding the result.

    Convert this Future[A] to a Future[Void] by discarding the result.

    Definition Classes
    Future
    Note

    failed futures will remain as is.

  74. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  75. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  76. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  77. def willEqual[B](that: Future[B]): Future[Boolean]

    Permalink

    Returns a Future[Boolean] indicating whether two Futures are equivalent.

    Returns a Future[Boolean] indicating whether two Futures are equivalent.

    Note that

    Future.exception(e).willEqual(Future.exception(e)) == Future.value(true)

    .

    Future.exception(e).willEqual(Future.exception(e)) == Future.value(true) }}}

    Definition Classes
    Future
  78. def withFilter(p: (A) ⇒ Boolean): Future[A]

    Permalink
    Definition Classes
    Future
  79. def within(timer: Timer, timeout: Duration, exc: ⇒ Throwable): Future[A]

    Permalink

    Returns a new Future that fails if it is not satisfied in time.

    Returns a new Future that fails if it is not satisfied in time.

    Note: On timeout, the underlying future is not interrupted.

    timer

    to run timeout on.

    timeout

    indicates how long you are willing to wait for the result to be available.

    exc

    exception to throw.

    Definition Classes
    Future
  80. def within(timer: Timer, timeout: Duration): Future[A]

    Permalink

    Returns a new Future that fails if it is not satisfied in time.

    Returns a new Future that fails if it is not satisfied in time.

    Note: On timeout, the underlying future is not interrupted.

    Definition Classes
    Future
  81. def within(timeout: Duration)(implicit timer: Timer): Future[A]

    Permalink

    Returns a new Future that fails if it is not satisfied in time.

    Returns a new Future that fails if it is not satisfied in time.

    Same as the other within, but with an implicit timer. Sometimes this is more convenient.

    Note: On timeout, the underlying future is not interrupted.

    Definition Classes
    Future

Inherited from Updatable[Try[A]]

Inherited from Responder[A]

Inherited from Future[A]

Inherited from Awaitable[A]

Inherited from AnyRef

Inherited from Any

Ungrouped