score:3
This behavior is actually documented for Future.andThen
:
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.
It means that map
will not start its job before the calculations inside andThen
are completed. If this is not what you want, you have to call map
on the original Future
. And then you can use onComplete
instead of andThen
, so code would become something like this:
val future = Future {
println("Started initial Future")
10
}
future onComplete { case Success(value) =>
println("Started callback")
Thread.sleep(2000)
println(s"Finished callback: value = $value")
}
val f2 = future map { x =>
println("Chained transformation")
x * 2
}
println(Await.result(f2, Duration.Inf))
P.S. AFAIK there is no standard onComplete
equivalent that can be used with method chaining and I think this is by design to make it easier to predict behavior by reading the code. Currently you can use a simple rule: if it is chained - it is executed later.
Source: stackoverflow.com
Related Query
- Throttling Scala Future blocks when onComplete is used
- Scala Future blocks transformations?
- Future investment: Erlang vs. Scala
- Is Future in Scala a monad?
- Nashorn and Scala future to JS Promise conversion
- Akka HTTP: Blocking in a future blocks the server
- ListenableFuture to scala Future
- Convert a Java Future to a Scala Future
- Scala Future with filter in for comprehension
- Understanding Scala Blocks
- Future with Timeout in Scala
- Get rid of Scala Future nesting
- Error handling Scala : Future For Comprehension
- What are the differences between a Scala Future and a Java Future
- What are advantages of a Twitter Future over a Scala Future?
- Convert scala future to java future
- Convert scala 2.10 future to scalaz.concurrent.Future // Task
- MongoDB scala driver: what is a best way to return Future when working with Observer callbacks?
- Converting Scala @suspendable Method into a Future
- Cancellation with Future and Promise in Scala
- Scala Future mapTo fails to compile because of missing ClassTag
- Why must forward referenced values inside blocks in Scala be lazy?
- convert Scala Future to Twitter Future
- Scala for comprehension with future and options
- Kill or timeout a Future in Scala 2.10
- Scala future sequence and timeout handling
- Apply several string transformations in scala
- Does a wait on Scala Future block thread?
- Scala Future for comprehension: sequential vs parallel
- replace a while loop with Future in scala
More Query from same tag
- Serializing a Scala List to JSON in Play2
- java.lang.IllegalArgumentException: requirement failed: No output operations registered, so nothing to execute
- Replace the values based on condition spark
- Scala parameterized type problem with returning an instance of the same type
- Tomcat / Scala / Lift: S.redirectTo redirects to wrong URL
- Recaptcha with scala and play framework
- How to create a List of values aggregation after a join on DataFrame elements?
- Merging configurations for spark using typesafe library and extraJavaOptions
- Removing the Option type from a joined RDD
- How to use akka-http-circe on client side calls?
- How to fix permission denied error when trying to run scala on OS X?
- Scala PartialFunction construction with different results
- Scala [Functional]: Find the actual indices of an element within a list that may be repeated
- Scala: type mismatch error
- Variance annotations in type aliases
- Circe decoder-encoder for object as json parameter type
- How to flatten a tuple, as flatMap is not defined over tuples?
- value sequence is not a member of cats.Applicative[F]
- Why does Scala's toSeq convert an immutable Set to a mutable ArrayBuffer?
- Pattern matching :: case class
- Play2: Difference between appDependencies and libraryDependencies?
- In Scala using an XML match expression I get the error: scala.MatchError. What am I doing wrong?
- transforming a Seq[Future[X]] into an Enumerator[X]
- How to Enable HSTS in Play framework 2.3.x using scala code?
- Receiving HTTP request params in Lift
- Indentation preserving string interpolation in scala
- Scala optimizing optional string parsing
- How to find the length of characters in a string using Scala Spark
- Privacy of inner classes and accessibility from subtypes of the outer type
- Multiple parameters lists and default arguments