Creates a new promise.
Creates a new promise.
When this future is completed, either through an exception, a timeout, or a value, apply the provided function.
When this future is completed, either through an exception, a timeout, 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 contained value of this Future.
The contained value of this Future. Before this Future is completed the value will be None. After completion the value will be Some(Right(t)) if it contains a valid result, or Some(Left(error)) if it contains an exception.
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) }
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 or times out, the resulting future also fails or times out, respectively.
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(0) g // evaluates to 5 await(0) h // throw a NoSuchElementException
Creates a new future which holds the result of either this future or that
future, depending on
which future was completed first.
Creates a new future which holds the result of either this future or that
future, depending on
which future was completed first.
Note: using this method yields nondeterministic dataflow programs.
Example:
val f = future { sys.error("failed") } val g = future { 5 } val h = f either g await(0) h // evaluates to either 5 or throws a runtime exception
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.
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 orElse g await(0) h // evaluates to 5
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 or times out, the resulting future also fails or times out, respectively.
Example:
val f = future { 5 } val g = f filter { _ % 2 == 1 } val h = f filter { _ % 2 == 0 } await(0) g // evaluates to 5 await(0) h // throw a NoSuchElementException
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.
$forComprehensionExample
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.
Tests whether this Future has been completed.
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.
$forComprehensionExample
Creates a new Future[A] which is completed with this Future's result if that conforms to A's erased type or a ClassCastException otherwise.
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 futuresInterruptedException
- not contained within futuresscala.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 instead from that throwable instead 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.
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.
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
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
Used by for-comprehensions.
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
.