Packages

abstract class Future[+A] extends Awaitable[A]

Represents an asynchronous value.

See the user guide on concurrent programming with Futures to better understand how they work and can be used.

A Future[T] can be in one of three states:

  • Pending, the computation has not yet completed
  • Satisfied successfully, with a result of type T (a Return)
  • Satisfied by a failure, with a Throwable result (a Throw)

This definition of Future does not assume any concrete implementation; in particular, it does not couple the user to a specific executor or event loop.

Self Type
Future[A]
See also

Futures for Java-friendly APIs.

The user guide on concurrent programming with Futures.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Future
  2. Awaitable
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Future()

Abstract Value Members

  1. abstract def isReady(implicit permit: CanAwait): Boolean

    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
    Awaitable
  2. abstract def poll: Option[Try[A]]

    Polls for an available result.

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

  3. abstract def raise(interrupt: Throwable): Unit

    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).

  4. abstract def ready(timeout: Duration)(implicit permit: CanAwait): Future.this.type

    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
    Awaitable
    Annotations
    @throws( classOf[TimeoutException] ) @throws( classOf[InterruptedException] )
  5. abstract def respond(k: (Try[A]) ⇒ Unit): Future[A]

    When the computation completes, invoke the given callback function.

    When the computation completes, invoke the given callback function.

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

    This method is most useful for very generic code (like libraries). Otherwise, it is a best practice to use one of the alternatives (onSuccess, onFailure, etc.).

    k

    the side-effect to apply when the computation completes. The value of the input to k will be the result of the computation to this future.

    returns

    a chained Future[A]

    Note

    this should be used for side-effects.

    See also

    addEventListener for a Java friendly API.

    ensure if you are not interested in the result of the computation.

    transform to produce a new Future from the result of the computation.

  6. abstract def result(timeout: Duration)(implicit permit: CanAwait): A

    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
    Awaitable
    Annotations
    @throws( classOf[Exception] )
  7. abstract def transform[B](f: (Try[A]) ⇒ Future[B]): Future[B]

    When this future completes, run f on that completed result whether or not this computation was successful.

    When this future completes, run f on that completed result whether or not this computation was successful.

    The returned Future will be satisfied when this, the original future, and f are done.

    See also

    transformedBy for a Java friendly API.

    handle and rescue for dealing strictly with exceptional computations.

    map and flatMap for dealing strictly with successful computations.

    respond for purely side-effecting callbacks.

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. def addEventListener(listener: FutureEventListener[_ >: A]): Future[A]

    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.

    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
    Definition Classes
    Any
  6. def before[B](f: ⇒ Future[B])(implicit ev: <:<[Future.this.type, Future[Unit]]): Future[B]

    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.

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

    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.

    timer

    to run timeout on.

    when

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

    exc

    exception to throw.

  8. def by(timer: Timer, when: Time): Future[A]

    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.

  9. def by(when: Time)(implicit timer: Timer): Future[A]

    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.

  10. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  11. def delayed(howlong: Duration)(implicit timer: Timer): Future[A]

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

  12. def ensure(f: ⇒ Unit): Future[A]

    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.

    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.

  13. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  15. def filter(p: (A) ⇒ Boolean): Future[A]
  16. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  17. def flatMap[B](f: (A) ⇒ Future[B]): Future[B]

    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.

    See also

    map

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

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

  19. def foreach(k: (A) ⇒ Unit): Future[A]

    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.

    See also

    onSuccess

  20. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
  21. def handle[B >: A](rescueException: PartialFunction[Throwable, B]): Future[B]

    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.

    See also

    rescue

  22. def hashCode(): Int
    Definition Classes
    AnyRef → Any
  23. def interruptible(): Future[A]

    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.

  24. def isDefined: Boolean

    Is the result of the Future available yet?

  25. def isDone(implicit ev: <:<[Future.this.type, Future[Unit]]): Boolean

    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.

  26. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  27. def join[B](other: Future[B]): Future[(A, B)]

    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.

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

    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)
  29. def liftToTry: Future[Try[A]]

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

  30. def lowerFromTry[B](implicit ev: <:<[A, Try[B]]): Future[B]

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

  31. def map[B](f: (A) ⇒ B): Future[B]

    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.

    See also

    onSuccess for side-effecting chained computations.

    flatMap for computations that return Futures.

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

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

  33. def masked: Future[A]

    Returns an identical future that ignores all interrupts

  34. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  35. final def notify(): Unit
    Definition Classes
    AnyRef
  36. final def notifyAll(): Unit
    Definition Classes
    AnyRef
  37. def onFailure(fn: (Throwable) ⇒ Unit): Future[A]

    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

    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.

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

    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

    Note

    this should be used for side-effects.

    See also

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

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

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

  40. def proxyTo[B >: A](other: Promise[B]): Unit

    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.

    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

  41. def raiseWithin(timer: Timer, timeout: Duration, exc: Throwable): Future[A]

    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.

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

    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.

  43. def raiseWithin(timeout: Duration)(implicit timer: Timer): Future[A]

    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.

  44. def rescue[B >: A](rescueException: PartialFunction[Throwable, Future[B]]): Future[B]

    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.

    See also

    handle

  45. def select[U >: A](other: Future[U]): Future[U]

    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

  46. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  47. def toJavaFuture: java.util.concurrent.Future[_ <: A]

    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)

  48. def toOffer: Offer[Try[A]]

    An Offer for this future.

    An Offer for this future.

    The offer is activated when the future is satisfied.

  49. def toString(): String
    Definition Classes
    AnyRef → Any
  50. def transformedBy[B](transformer: FutureTransformer[A, B]): Future[B]

    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.

    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.

  51. def unit: Future[Unit]

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

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

    Note

    failed futures will remain as is.

  52. def voided: Future[Void]

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

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

    Note

    failed futures will remain as is.

  53. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  54. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  55. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  56. def willEqual[B](that: Future[B]): Future[Boolean]

    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) }}}

  57. def withFilter(p: (A) ⇒ Boolean): Future[A]
  58. def within(timer: Timer, timeout: Duration, exc: ⇒ Throwable): Future[A]

    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.

  59. def within(timer: Timer, timeout: Duration): Future[A]

    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.

  60. def within(timeout: Duration)(implicit timer: Timer): Future[A]

    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.

Inherited from Awaitable[A]

Inherited from AnyRef

Inherited from Any

Ungrouped