score:1
As you mentioned blocking is bad and you only should use it in testing.
Here is a nice Blog that describes the possibilities: testing-future-objects-scalatest
In real life you should use for example this:
futureResult.foreach { x: String =>
// do some side effect
}
All you have to make sure is that the process that calls this Future is not stopped (that is for example in a test the case)
Another solution is to use the Async - Await construct that has alos an implementation in Scala: scala-async
score:4
The best practice is to do a Await "at the end of the world". This means that you should combine all your futures in a single future and then Await only once.
val f1 = (ping1 ? AskingTest).mapTo[String]
val f2 = (ping2 ? AskingTest).mapTo[String]
val combinedFuture: Future[(String, String)] = for {
str1 <- f1
str2 <- f2
} yield (str1, str2)
// final await at the end of program
val (str1, str2) = Await.result(combinedFuture, Duration.Inf)
When I started to write code with Futures, I used to write Await at every line. which is wrong because it defeats the purpose of using Futures.
So your goal is to combine as many futures as possible using for
and then Await only once.
The other solutions you have listed above are also OK except No 4 because it can lead to your program crashing if the future is not complete yet.
Source: stackoverflow.com
Related Query
- Get internal data of Scala Future **BEST CASE**
- How can I get random data generated for scala case classes with the ability to "change some values" for unit testing?
- What is best practice to represent data by Scala case classes
- How can I easily get a Scala case class's name?
- How to get around the Scala case class limit of 22 fields?
- Scala - No TypeTag Available Exception when using case class to try to get TypeTag?
- Get rid of Scala Future nesting
- Can I get a Scala case class definition from an Avro schema definition?
- Which is best data access options available for Play framework with Scala and PostgreSQL?
- MongoDB scala driver: what is a best way to return Future when working with Observer callbacks?
- Scala case class vs Kotlin data class
- Spark Scala Get Data Back from rdd.foreachPartition
- Is there a way to get a JSON-Schema from a Scala Case Class hierarchy?
- Best way to reflect case class fields in scala 2.11?
- Getting data out of a Future in Scala
- Best way to perform case insensitive comparison for Scala case class
- Return a string from a Future onComplete case with Scala and Spray.io
- scala map get the value by key with key case insensitive
- Best practice to make cleanup after Scala Future is complete
- Multiple Futures in Play and using case classes to hold the future data
- What's the best way to extract a value from a scala case class?
- Scala case classes and tail recursion best practices
- Get Future objects from Future Options in Scala
- Scala - best data structure to store a trie
- Get best parameters for TrainValidationSplit scala
- What's the best way to serialize a scala case class that has elements that aren't serializable?
- Scala compare case class and get changed fields
- What is the best way of letting Future values, into case class constructer, that doesn't accept futures?
- Scala best practices: mapping 2D data
- To get the list of filename stored in azure data lake through scala
More Query from same tag
- Multiple scala data frame concat
- Playframework FakeRequest withFormUrlEncodedBody data is lost
- Scala mutator naming conventions and requirements
- Maven build throwing an error "File not exists"
- How to make Java asserts work in sbt project?
- Why does Spark RDD partition has 2GB limit for HDFS?
- How to map an Option inside a for comprehension with EitherT
- What advantages does scala.util.Try have over try..catch?
- Introducing scala into existing netbeans java project
- How to setup build.sbt with sbt-assembly plugin?
- is there a way to mock Instant.now
- Kafka topic to websocket
- Scala: currying vs closure
- Unable to resolve mock[....] in playframework 2.6.17
- How to convert my RDD of JSON strings to DataFrame
- Scala: strange performance of scala programs in IntelliJ IDEA
- DFS performance of Spark GraphX vs simple Java DFS implementation
- Uncaught exception during compilation: java.lang.AssertionError
- Problem with developing Engines with IntelliJ IDEA
- Why does sbt keep downloading my snapshot dependencies?
- In Scala, how can I check if a generic HashMap contains a particular key?
- Scala Variable Pattern: variable is not assigned
- scala is inferring Nothing though defining with explicit types
- Spark RDD[(String, String)] into RDD[Map[String, String]]
- Scala quasiquote macro example broken - type signatures off
- Recreating the behavior of Haskell's `replicateM` in Scala
- Class type required but T found
- for comprehension with "case-like" type check
- self actor hangs in repl
- Restart PlayConsole SBT server