Returns whether the future had already been completed with a value or an exception.
Note: using this method yields nondeterministic dataflow programs.
true
if the future was completed, false
otherwise
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.
Note that the returned value of f
will be discarded.
Since this method executes asynchronously and does not produce a return value, any non-fatal exceptions thrown will be reported to the ExecutionContext
.
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. Completion of the Future must *happen-before* the invocation of the callback.
only used to accept any return type of the given callback function
the function to be executed when this Future
completes
Await the "completed" state of this Awaitable
.
This method should not be called directly; use Await.ready instead.
maximum wait time, which may be negative (no waiting is done), Duration.Inf for unbounded waiting, or a finite positive duration
this Awaitable
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
Await and return the result (of type T
) of this Awaitable
.
This method should not be called directly; use Await.result instead.
maximum wait time, which may be negative (no waiting is done), Duration.Inf for unbounded waiting, or a finite positive duration
the result value if the Awaitable
is completed within the specific maximum wait time
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
Creates a new Future by applying the specified function to the result of this Future. If there is any non-fatal exception thrown when 'f' is applied then that exception will be propagated to the resulting future.
the type of the returned Future
function that transforms the result of this future
a Future
that will be completed with the transformed value
Creates a new Future by applying the specified function, which produces a Future, to the result of this Future. If there is any non-fatal exception thrown when 'f' is applied then that exception will be propagated to the resulting future.
the type of the returned Future
function that transforms the result of this future
a Future
that will be completed with the transformed value
The current value of this Future
.
Note: using this method yields nondeterministic dataflow programs.
If the future was not completed the returned value will be None
. If the future was completed the value will be Some(Success(t))
if it contained a valid result, or Some(Failure(error))
if it contained an exception.
None
if the Future
wasn't completed, Some
if it was.
Test two objects for inequality.
true
if !(this == that), false otherwise.
Equivalent to x.hashCode
except for boxed numeric types and null
. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. For null
returns a hashcode where null.hashCode
throws a NullPointerException
.
a hash value consistent with ==
The expression x == that
is equivalent to if (x eq null) that eq null else x.equals(that)
.
true
if the receiver object is equivalent to the argument; false
otherwise.
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 => throw new RuntimeException("runtime exception") } andThen { case Failure(t) => println(t) case Success(v) => println(v) }
Since this method executes asynchronously and does not produce a return value, any non-fatal exceptions thrown will be reported to the ExecutionContext
.
only used to accept any return type of the given PartialFunction
a PartialFunction
which will be conditionally applied to the outcome of this Future
a Future
which will be completed with the exact same outcome as this Future
but after the PartialFunction
has been executed.
Cast the receiver object to be of type T0
.
Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String]
will throw a ClassCastException
at runtime, while the expression List(1).asInstanceOf[List[String]]
will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.
the receiver object.
ClassCastException
if the receiver object is not an instance of the erasure of type T0
.
Create a copy of the receiver object.
The default implementation of the clone
method is platform dependent.
a copy of the receiver object.
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 } g foreach println // Eventually prints 5 Await.result(h, Duration.Zero) // throw a NoSuchElementException
the type of the returned Future
the PartialFunction
to apply to the successful result of this Future
a Future
holding the result of application of the PartialFunction
or a NoSuchElementException
Tests whether the argument (that
) is a reference to the receiver object (this
).
The eq
method implements an equivalence relation on non-null instances of AnyRef
, and has three additional properties:
x
and y
of type AnyRef
, multiple invocations of x.eq(y)
consistently returns true
or consistently returns false
.For any non-null instance x
of type AnyRef
, x.eq(null)
and null.eq(x)
returns false
.
null.eq(null)
returns true
. When overriding the equals
or hashCode
methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2
), they should be equal to each other (o1 == o2
) and they should hash to the same value (o1.hashCode == o2.hashCode
).
true
if the argument is a reference to the receiver object; false
otherwise.
The equality method for reference types. Default implementation delegates to eq
.
See also equals
in scala.Any.
true
if the receiver object is equivalent to the argument; false
otherwise.
The returned Future
will be successfully completed with the Throwable
of the original Future
if the original Future
fails.
If the original Future
is successful, the returned Future
is failed with a NoSuchElementException
.
This 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
- fatal errors are not contained within futures
InterruptedException
- not contained within futuresall 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.
a failed projection of this Future
.
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 { throw new RuntimeException("failed") } val g = Future { 5 } val h = f fallbackTo g h foreach println // Eventually prints 5
the type of the other Future
and the resulting Future
the Future
whose result we want to use if this Future
fails.
a Future
with the successful result of this or that Future
or the failure of this Future
if both fail
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 } g foreach println // Eventually prints 5 Await.result(h, Duration.Zero) // throw a NoSuchElementException
the predicate to apply to the successful result of this Future
a Future
which will hold the successful result of this Future
if it matches the predicate or a NoSuchElementException
Called by the garbage collector on the receiver object when there are no more references to the object.
The details of when and if the finalize
method is invoked, as well as the interaction between finalize
and non-local returns and exceptions, are all platform dependent.
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(3) } yield x + y
is translated to:
f flatMap { (x: Int) => g map { (y: Int) => x + y } }
the type of the returned Future
the function which will be applied to the successful result of this Future
a Future
which will be completed with the result of the application of the function
Creates a new future with one level of nesting flattened, this method is equivalent to flatMap(identity)
.
the type of the returned Future
Asynchronously processes the value in the future once the value becomes available.
WARNING: Will not be called if this future is never completed or if it is completed with a failure.
Since this method executes asynchronously and does not produce a return value, any non-fatal exceptions thrown will be reported to the ExecutionContext
.
only used to accept any return type of the given callback function
the function which will be executed if this Future
completes with a result, the return value of f
will be discarded.
Returns string formatted according to given format
string. Format strings are as for String.format
(@see java.lang.String.format).
Returns the runtime class representation of the object.
a class object corresponding to the runtime type of the receiver.
The hashCode method for reference types. See hashCode in scala.Any.
the hash code value for this object.
Test whether the dynamic type of the receiver object is T0
.
Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String]
will return false
, while the expression List(1).isInstanceOf[List[String]]
will return true
. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.
true
if the receiver object is an instance of erasure of type T0
; false
otherwise.
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 { "The future" } val g = f map { x: String => x + " is now!" }
Note that a for comprehension involving a Future
may expand to include a call to map
and or flatMap
and withFilter
. See scala.concurrent.Future#flatMap for an example of such a comprehension.
the type of the returned Future
the function which will be applied to the successful result of this Future
a Future
which will be completed with the result of the application of the function
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.
the type of the returned Future
the ClassTag
which will be used to cast the result of this Future
a Future
holding the casted result of this Future
or a ClassCastException
otherwise
Equivalent to !(this eq that)
.
true
if the argument is not a reference to the receiver object; false
otherwise.
Wakes up a single thread that is waiting on the receiver object's monitor.
not specified by SLS as a member of AnyRef
Wakes up all threads that are waiting on the receiver object's monitor.
not specified by SLS as a member of AnyRef
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
the type of the returned Future
the PartialFunction
to apply if this Future
fails
a Future
with the successful value of this Future
or the result of the PartialFunction
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
the type of the returned Future
the PartialFunction
to apply if this Future
fails
a Future
with the successful value of this Future
or the outcome of the Future
returned by the PartialFunction
Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.
a String representation of the object.
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.
the type of the returned Future
function that transforms a successful result of the receiver into a successful result of the returned future
function that transforms a failure of the receiver into a failure of the returned future
a Future
that will be completed with the transformed value
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
.
the type of the other Future
the other Future
a Future
with the results of both futures or the failure of the first of them that failed
Zips the values of this
and that
future using a function f
, and creates a new future holding the result.
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
. If the application of f
throws a throwable, the resulting future is failed with that throwable if it is non-fatal.
the type of the other Future
the type of the resulting Future
the other Future
the function to apply to the results of this
and that
a Future
with the result of the application of f
to the results of this
and that
© 2002-2019 EPFL, with contributions from Lightbend.
Licensed under the Apache License, Version 2.0.
https://www.scala-lang.org/api/2.13.0/scala/concurrent/Future.html
A
Future
represents a value which may or may not *currently* be available, but will be available at some point, or an exception if that value could not be made available.Asynchronous computations that yield futures are created with the
Future.apply
call and are computed using a suppliedExecutionContext
, which can be backed by a Thread pool.Futures and Promises