score:3
Generally when working with Future
s, you'll want to do any further processing via methods called on the returned Future
. Eg.:
val newUser = User(7, "someName")
val future = userRepo.insertOrUpdate(newUser)
future.onSuccess { outcome => // Here, 'outcome' will hold whatever was contained in the Future - Unit in your description above.
val storedUser = userRepo.findById(7) // This will only execute once the future completes (successfully).
...
}
There are plenty of other useful methods for manipulating a Future
(or a collection of them), such as "onFailure", "recover", "map" and "flatMap".
Try not to wait on the Future
until as late as possible - preferably let Play or Spray or whatever other framework you might happen to be using take care of it for you (see here for Play documentation on doing this, for example).
Finally, in terms of your DB call to insert, I'd look into having the call return at least a boolean, or better still the primary key the new entry was inserted with, rather than Unit
.
Source: stackoverflow.com
Related Query
- How to Design and Call Scala API with Futures
- How to stub a method call with an implicit matcher in Mockito and Scala
- Scala - How to Call REST API and save as json file in HDFS?
- Choosing between side effects and good API design in functional programming with scala
- How to map multiple Futures and pass them as arguments to a view using Play with Scala
- How can I call Scala function with class name and function name as strings
- Scala classOf for type parameter: how to call the function and how to restrict with upper type bounds
- How to call a Scala function with a single parameter and write the parameter first
- Service class and logging design Scala with Futures
- How to override generic method in scala and call it with reflection
- How to iterate through json array in Scala and apply API call for each json element
- How to use IntelliJ with Play Framework and Scala
- How do I disambiguate in Scala between methods with vararg and without
- How to get logging working in scala unit tests with testng, slf4s, and logback
- How to find a matching element in a list and map it in as an Scala API method?
- How do I get Intellij IDEA 12.0 to work with Play Framework 2.1.0 app and Scala 2.10.0?
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- How to know if a Scala file modified with IntelliJ Idea is saved and if it is checked into CVS?
- How are Scala Futures chained together with flatMap?
- How do you run cucumber with Scala 2.11 and sbt 0.13?
- Scala (Play 2.4.x) How to call a class with @inject() annotation
- How to obfuscate fat Scala Jar with Proguard and SBT
- How to call superclass constructor from child class in scala and how to do constructor chaining
- How to use play-plugins-mailer with Play 2.3 and Scala 2.11?
- How to call Stored Procedures and defined functions in MySQL with Slick 3.0
- How to generate Scala setters and getters with IntelliJ IDEA
- Is there a way to call a function defined using `val` in Scala with the whole curly brace block as an argument and not the final result of that block?
- Deadlock with synchronized and scala futures
- How programmatically limit download speed on server with Scala and Play
- How do I create horizontal or vertical struts and glue for use with scala BoxPanel?
More Query from same tag
- Can I use Scala options to avoid the following NoSuchElementException?
- Scala Generic Type slow
- scala.collection.mutable.PriorityQueue: Comparing with 2 or more attributes of a case class
- How to initialize the value from trait in subtype?
- Array IndexOutOfBound Exception while filtering spark RDD
- How to resolve Kafka Consumer poll timeout error
- Filtering on Scala Option[Set]
- Got NullPointerException, when I using SparkStreaming to consume the Kafka's messages
- Global Exception Handling in Play Application
- Implicit resolution failure on import of implicit value with same name as method in class
- SBT Native Packager - How to retrieve libraryDependencies and store them as part of the package?
- How to convert a supplier to Stream?
- How do I cast Any returned from executeInsert in Anorm to Long
- What is the advantage of using Option.map over Option.isEmpty and Option.get?
- import class priority in scala vs java
- XML Library Missing in Scala Eclipse Distribution
- How make tests always run in same order in Scalatest?
- How to filter Chinese punctuation in Scala/Java?
- Unable to find JSONReader for parameterised-typed custom class in test, despite import
- short way to breakOut to specific collection type?
- RGB Image doesn't load correctly from ByteArray: opencv java (wrapped in scala)
- Could not find implicit value for parameter um
- change return type of the superclass method
- Lift Req object
- Mongodb + elasticsearch + scala lift
- Make an actor sleep
- How to use the functions.explode to flatten element in dataFrame
- Scala Tuples, Manipulating Map Data
- Specify different version of sbt for a subproject in multi-project?
- Spark streaming does not print some data