scala.concurrent

Future

trait Future[+T] extends Awaitable[T]

The trait that represents futures.

Asynchronous computations that yield futures are created with the future call:

val s = "Hello"
val f: Future[String] = future {
  s + " future!"
}
f onSuccess {
  case msg => println(msg)
}
Source
Future.scala
Linear Supertypes
Awaitable[T], AnyRef, Any
Type Hierarchy Learn more about scaladoc diagrams
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Future
  2. Awaitable
  3. AnyRef
  4. Any
Implicitly
  1. by StringAdd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def isCompleted: Boolean

    Returns whether the future has already been completed with a value or an exception.

    Returns whether the future has already been completed with a value or an exception.

    Note: using this method yields nondeterministic dataflow programs.

    returns

    true if the future is already completed, false otherwise

  2. abstract def onComplete[U](func: (Try[T]) ⇒ U)(implicit executor: ExecutionContext): Unit

    When this future is completed, either through an exception, or a value, apply the provided function.

    When this future is completed, either through an exception, or a value, apply the provided function.

    If the future has already been completed, this will either be applied immediately or be scheduled asynchronously.

    Multiple callbacks may be registered; there is no guarantee that they will be executed in a particular order.

    The provided callback always runs in the provided implicit ExecutionContext, though there is no guarantee that the execute() method on the ExecutionContext will be called once per callback or that execute() will be called in the current thread. That is, the implementation may run multiple callbacks in a batch within a single execute() and it may run execute() either immediately or asynchronously.

  3. abstract def ready(atMost: Duration)(implicit permit: CanAwait): Future.this.type

    Await the "completed" state of this Awaitable.

    Await the "completed" state of this Awaitable.

    This method should not be called directly; use Await.ready instead.

    atMost

    maximum wait time, which may be negative (no waiting is done), Duration.Inf for unbounded waiting, or a finite positive duration

    returns

    this Awaitable

    Definition Classes
    Awaitable
    Annotations
    @throws( cause = classOf[TimeoutException] ) @throws( cause = classOf[InterruptedException] )
    Exceptions thrown
    IllegalArgumentException

    if atMost is Duration.Undefined

    InterruptedException

    if the current thread is interrupted while waiting

    TimeoutException

    if after waiting for the specified time this Awaitable is still not ready

  4. abstract def result(atMost: Duration)(implicit permit: CanAwait): T

    Await and return the result (of type T) of this Awaitable.

    Await and return the result (of type T) of this Awaitable.

    This method should not be called directly; use Await.result instead.

    atMost

    maximum wait time, which may be negative (no waiting is done), Duration.Inf for unbounded waiting, or a finite positive duration

    returns

    the result value if the Awaitable is completed within the specific maximum wait time

    Definition Classes
    Awaitable
    Annotations
    @throws( cause = classOf[Exception] )
    Exceptions thrown
    IllegalArgumentException

    if atMost is Duration.Undefined

    InterruptedException

    if the current thread is interrupted while waiting

    TimeoutException

    if after waiting for the specified time this Awaitable is still not ready

  5. abstract def value: Option[Try[T]]

    The value of this Future.

    The value of this Future.

    If the future is not completed the returned value will be None. If the future is completed the value will be Some(Success(t)) if it contains a valid result, or Some(Failure(error)) if it contains an exception.

Concrete Value Members

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

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. def +(other: String): String

    Implicit information
    This member is added by an implicit conversion from Future[T] to StringAdd[Future[T]] performed by method StringAdd in scala.Predef.
    Definition Classes
    StringAdd
  5. def ->[B](y: B): (Future[T], B)

    Implicit information
    This member is added by an implicit conversion from Future[T] to ArrowAssoc[Future[T]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  6. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  7. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  8. def andThen[U](pf: PartialFunction[Try[T], U])(implicit executor: ExecutionContext): Future[T]

    Applies the side-effecting function to the result of this future, and returns a new future with the result of this future.

    Applies the side-effecting function to the result of this future, and returns a new future with the result of this future.

    This method allows one to enforce that the callbacks are executed in a specified order.

    Note that if one of the chained andThen callbacks throws an exception, that exception is not propagated to the subsequent andThen callbacks. Instead, the subsequent andThen callbacks are given the original value of this future.

    The following example prints out 5:

    val f = future { 5 }
    f andThen {
      case r => sys.error("runtime exception")
    } andThen {
      case Failure(t) => println(t)
      case Success(v) => println(v)
    }
  9. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  10. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  11. def collect[S](pf: PartialFunction[T, S])(implicit executor: ExecutionContext): Future[S]

    Creates a new future by mapping the value of the current future, if the given partial function is defined at that value.

    Creates a new future by mapping the value of the current future, if the given partial function is defined at that value.

    If the current future contains a value for which the partial function is defined, the new future will also hold that value. Otherwise, the resulting future will fail with a NoSuchElementException.

    If the current future fails, then the resulting future also fails.

    Example:

    val f = future { -5 }
    val g = f collect {
      case x if x < 0 => -x
    }
    val h = f collect {
      case x if x > 0 => x * 2
    }
    Await.result(g, Duration.Zero) // evaluates to 5
    Await.result(h, Duration.Zero) // throw a NoSuchElementException
  12. def ensuring(cond: (Future[T]) ⇒ Boolean, msg: ⇒ Any): Future[T]

    Implicit information
    This member is added by an implicit conversion from Future[T] to Ensuring[Future[T]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def ensuring(cond: (Future[T]) ⇒ Boolean): Future[T]

    Implicit information
    This member is added by an implicit conversion from Future[T] to Ensuring[Future[T]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  14. def ensuring(cond: Boolean, msg: ⇒ Any): Future[T]

    Implicit information
    This member is added by an implicit conversion from Future[T] to Ensuring[Future[T]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  15. def ensuring(cond: Boolean): Future[T]

    Implicit information
    This member is added by an implicit conversion from Future[T] to Ensuring[Future[T]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  16. final def eq(arg0: AnyRef): Boolean

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

    Definition Classes
    AnyRef → Any
  18. def failed: Future[Throwable]

    Returns a failed projection of this future.

    Returns a failed projection of this future.

    The failed projection is a future holding a value of type Throwable.

    It is completed with a value which is the throwable of the original future in case the original future is failed.

    It is failed with a NoSuchElementException if the original future is completed successfully.

    Blocking on this future returns a value if the original future is completed with an exception and throws a corresponding exception if the original future fails.

  19. def fallbackTo[U >: T](that: Future[U]): Future[U]

    Creates a new future which holds the result of this future if it was completed successfully, or, if not, the result of the that future if that is completed successfully.

    Creates a new future which holds the result of this future if it was completed successfully, or, if not, the result of the that future if that is completed successfully. If both futures are failed, the resulting future holds the throwable object of the first future.

    Using this method will not cause concurrent programs to become nondeterministic.

    Example:

    val f = future { sys.error("failed") }
    val g = future { 5 }
    val h = f fallbackTo g
    Await.result(h, Duration.Zero) // evaluates to 5
  20. def filter(pred: (T) ⇒ Boolean)(implicit executor: ExecutionContext): Future[T]

    Creates a new future by filtering the value of the current future with a predicate.

    Creates a new future by filtering the value of the current future with a predicate.

    If the current future contains a value which satisfies the predicate, the new future will also hold that value. Otherwise, the resulting future will fail with a NoSuchElementException.

    If the current future fails, then the resulting future also fails.

    Example:

    val f = future { 5 }
    val g = f filter { _ % 2 == 1 }
    val h = f filter { _ % 2 == 0 }
    Await.result(g, Duration.Zero) // evaluates to 5
    Await.result(h, Duration.Zero) // throw a NoSuchElementException
  21. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  22. def flatMap[S](f: (T) ⇒ Future[S])(implicit executor: ExecutionContext): Future[S]

    Creates a new future by applying a function to the successful result of this future, and returns the result of the function as the new future.

    Creates a new future by applying a function to the successful result of this future, and returns the result of the function as the new future. If this future is completed with an exception then the new future will also contain this exception.

    Example:

    val f = future { 5 }
    val g = future { 3 }
    val h = for {
      x: Int <- f // returns Future(5)
      y: Int <- g // returns Future(5)
    } yield x + y

    is translated to:

    f flatMap { (x: Int) => g map { (y: Int) => x + y } }
  23. def foreach[U](f: (T) ⇒ U)(implicit executor: ExecutionContext): Unit

    Asynchronously processes the value in the future once the value becomes available.

    Asynchronously processes the value in the future once the value becomes available.

    Will not be called if the future fails.

  24. def formatted(fmtstr: String): String

    Returns string formatted according to given format string.

    Returns string formatted according to given format string. Format strings are as for String.format (@see java.lang.String.format).

    Implicit information
    This member is added by an implicit conversion from Future[T] to StringFormat[Future[T]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  25. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  26. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  27. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  28. def map[S](f: (T) ⇒ S)(implicit executor: ExecutionContext): Future[S]

    Creates a new future by applying a function to the successful result of this future.

    Creates a new future by applying a function to the successful result of this future. If this future is completed with an exception then the new future will also contain this exception.

    Example:

    val f = future { 5 }
    val g = future { 3 }
    val h = for {
      x: Int <- f // returns Future(5)
      y: Int <- g // returns Future(5)
    } yield x + y

    is translated to:

    f flatMap { (x: Int) => g map { (y: Int) => x + y } }
  29. def mapTo[S](implicit tag: ClassTag[S]): Future[S]

    Creates a new Future[S] which is completed with this Future's result if that conforms to S's erased type or a ClassCastException otherwise.

  30. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  31. final def notify(): Unit

    Definition Classes
    AnyRef
  32. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  33. def onFailure[U](callback: PartialFunction[Throwable, U])(implicit executor: ExecutionContext): Unit

    When this future is completed with a failure (i.

    When this future is completed with a failure (i.e. with a throwable), apply the provided callback to the throwable.

    The future may contain a throwable object and this means that the future failed. Futures obtained through combinators have the same exception as the future they were obtained from. The following throwable objects are not contained in the future:

    • Error - errors are not contained within futures
    • InterruptedException - not contained within futures
    • all scala.util.control.ControlThrowable except NonLocalReturnControl - not contained within futures

    Instead, the future is completed with a ExecutionException with one of the exceptions above as the cause. If a future is failed with a scala.runtime.NonLocalReturnControl, it is completed with a value from that throwable instead.

    If the future has already been completed with a failure, this will either be applied immediately or be scheduled asynchronously.

    Will not be called in case that the future is completed with a value.

    Multiple callbacks may be registered; there is no guarantee that they will be executed in a particular order.

    The provided callback always runs in the provided implicit ExecutionContext, though there is no guarantee that the execute() method on the ExecutionContext will be called once per callback or that execute() will be called in the current thread. That is, the implementation may run multiple callbacks in a batch within a single execute() and it may run execute() either immediately or asynchronously.

  34. def onSuccess[U](pf: PartialFunction[T, U])(implicit executor: ExecutionContext): Unit

    When this future is completed successfully (i.

    When this future is completed successfully (i.e. with a value), apply the provided partial function to the value if the partial function is defined at that value.

    If the future has already been completed with a value, this will either be applied immediately or be scheduled asynchronously.

    Multiple callbacks may be registered; there is no guarantee that they will be executed in a particular order.

    The provided callback always runs in the provided implicit ExecutionContext, though there is no guarantee that the execute() method on the ExecutionContext will be called once per callback or that execute() will be called in the current thread. That is, the implementation may run multiple callbacks in a batch within a single execute() and it may run execute() either immediately or asynchronously.

  35. def recover[U >: T](pf: PartialFunction[Throwable, U])(implicit executor: ExecutionContext): Future[U]

    Creates a new future that will handle any matching throwable that this future might contain.

    Creates a new future that will handle any matching throwable that this future might contain. If there is no match, or if this future contains a valid result then the new future will contain the same.

    Example:

    future (6 / 0) recover { case e: ArithmeticException => 0 } // result: 0
    future (6 / 0) recover { case e: NotFoundException   => 0 } // result: exception
    future (6 / 2) recover { case e: ArithmeticException => 0 } // result: 3
  36. def recoverWith[U >: T](pf: PartialFunction[Throwable, Future[U]])(implicit executor: ExecutionContext): Future[U]

    Creates a new future that will handle any matching throwable that this future might contain by assigning it a value of another future.

    Creates a new future that will handle any matching throwable that this future might contain by assigning it a value of another future.

    If there is no match, or if this future contains a valid result then the new future will contain the same result.

    Example:

    val f = future { Int.MaxValue }
    future (6 / 0) recoverWith { case e: ArithmeticException => f } // result: Int.MaxValue
  37. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  38. def toString(): String

    Definition Classes
    AnyRef → Any
  39. def transform[S](s: (T) ⇒ S, f: (Throwable) ⇒ Throwable)(implicit executor: ExecutionContext): Future[S]

    Creates a new future by applying the 's' function to the successful result of this future, or the 'f' function to the failed result.

    Creates a new future by applying the 's' function to the successful result of this future, or the 'f' function to the failed result. If there is any non-fatal exception thrown when 's' or 'f' is applied, that exception will be propagated to the resulting future.

    s

    function that transforms a successful result of the receiver into a successful result of the returned future

    f

    function that transforms a failure of the receiver into a failure of the returned future

    returns

    a future that will be completed with the transformed value

  40. final def wait(): Unit

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws()
  43. final def withFilter(p: (T) ⇒ Boolean)(implicit executor: ExecutionContext): Future[T]

    Used by for-comprehensions.

  44. def zip[U](that: Future[U]): Future[(T, U)]

    Zips the values of this and that future, and creates a new future holding the tuple of their results.

    Zips the values of this and that future, and creates a new future holding the tuple of their results.

    If this future fails, the resulting future is failed with the throwable stored in this. Otherwise, if that future fails, the resulting future is failed with the throwable stored in that.

  45. def [B](y: B): (Future[T], B)

    Implicit information
    This member is added by an implicit conversion from Future[T] to ArrowAssoc[Future[T]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from Awaitable[T]

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion StringAdd from Future[T] to StringAdd[Future[T]]

Inherited by implicit conversion StringFormat from Future[T] to StringFormat[Future[T]]

Inherited by implicit conversion Ensuring from Future[T] to Ensuring[Future[T]]

Inherited by implicit conversion ArrowAssoc from Future[T] to ArrowAssoc[Future[T]]

Ungrouped