Two other answers are misleading. A for yield in Scala is a compiler primitive that converts to map or flatMap . Do not use Await , if you can avoid this, this is not a simple problem.
You enter the locking behavior, and you still have to realize the system damage that you do when locking.
When it comes to Future , map and flatMap do different things:
the card is executed when the future ends. This is an asynchronous way to create safe type mappings.
val f: Future[A] = someFutureProducer def convertAToB(a: A): B = {..} f map { a => convertAToB(a) }
flatMap
is what you use for a chain of things:
someFuture flatMap { _ => { someOtherFuture } }
The above equivalent:
for { result1 <- someFuture result2 <- someOtherFuture } yield result2
On Play, you would use Async to handle the above:
Async { someFuture.map(i => Ok("Got result: " + i)) }
Update
I misunderstood your use of Play. However, this does not change anything. You can still make your logic asynchronous.
someFuture onComplete { case Success(result) =>
The main difference is when you asynchronously think that you should always map and flatMap and do everything else inside Future so that everything is done. The increase in productivity is massive.
The larger your application, the greater the gain.
source share