score:3
it does not work sequentially, it just cannot start the future
s till they are created (which would happen in your case in your flatmap
of your previous future
), so you need to create them in advance if you want to process them in parallel (with the usual implicit executioncontext
s).
probably this tutorial explains better though (it complicates it with withfilter
):
the purchase future is completed only once both usdquote and chfquote are completed– it depends on the values of both these futures so its own computation cannot begin earlier.
the for-comprehension above is translated into:
val purchase = usdquote flatmap { usd => chfquote
.withfilter(chf => isprofitable(usd, chf))
.map(chf => connection.buy(amount, chf)) }
which is a bit harder to grasp than the for-comprehension, but we analyze it to better understand the flatmap operation. the flatmap operation maps its own value into some other future. once this different future is completed, the resulting future is completed with its value. in our example, flatmap uses the value of the usdquote future to map the value of the chfquote into a third future which sends a request to buy a certain amount of swiss francs. the resulting future purchase is completed only once this third future returned from map completes.
what you would really only need is something like map2
instead of flatmap
, as you do not use the returned value from the previous future
to create the new future
.
Source: stackoverflow.com
Related Query
- Unable to use for comprehension to map over List within Future
- Scala Future with filter in for comprehension
- Error handling Scala : Future For Comprehension
- Scala for comprehension with future and options
- scala's for yield comprehension used with Future. How to wait until future has returned?
- understanding why future result with for comprehension yields Future[Nothing]
- Why Scala for comprehension run Future functions sequentially?
- Adding retry to future sequence for running Databricks notebooks in parallel in Scala
- Ignore failing future in for comprehension
- Scala future for comprehension with another future in yield
- Scala Future for Comprehension
- Scala: How to deal with sequences in a Future for comprehension
- Scala filter on future of boolean inside for comprehension
- Why the result type of unwrapping future via for comprehension is Future?
- Mixing Future and F[] inside a for comprehension
- How to wrap Scala for comprehension to resolve Future around Play's Form folding?
- Scala future for comprehension chain futures and results
- Scala for comprehension of sequence inside a Try
- Wait future completion to execute a another one for a sequence
- Scala for comprehension - chaining Future with IndexedSeq
- Running scala Futures conncurrently in for comprehension without future variables in local scope
- Future and Option in for comprehension in slick
- Future call in for comprehension in Scala - how do I process them sequentially?
- Unable to use for comprehension to resolve Future
- Scala for comprehension how to avoid creating of Future when passing results
- for comprehension from flatMap and future to future
- Scala waiting for sequence of futures
- Method parameters validation in Scala, with for comprehension and monads
- Map a Future for both Success and Failure
- Scala "<-" for comprehension
More Query from same tag
- How to match exactly 'n' given characters with FastParse
- Parse and Show the data of a JSON file in Scala | Meaning of .config("spark.some.config.option", "some-value").getOrCreate()
- Get data from database in form of JSON in PLAY(Scala)
- Cache an intermediate variable in an one-liner
- How do i populate a database with table/row definitions that were autogenerated?
- Could not find implicit value for parameter flash
- ClassNotFoundException:scala.PreDef$ issue
- Finding uncommon elements in Seq of Tuples
- scala parser ^^^ followed by a function
- Spark hangs on union with zero running task
- Factorial in Scala & Haskell
- Scala in IntelliJ error
- Spark sql Unix timestamp returning wrong output
- Getting an error in intellij referencing a scala method I'm not actually using in a class I have used
- SBT 0.13.8 what does the SettingKey.~= method do
- Tired of non-semantic testing to make up for dynamic typing - suggestions?
- Scala plugin in IDEA dosn't work
- How do I mock a Scala Companion Object with EASYMOCK on Scala 3?
- What does override do here?
- Make recursive function stack - safe with Free Monads Trampoline in Scala?
- Scala Bash test scripts
- Slick mapper fails, but didnt' see any exception
- Scaladocs: Reference values of enums
- Wrapping null-returning method in Java with Option in Scala?
- Implement a static website into play
- Spark Streaming avoid check-point serialization issue when re-deploying the code
- Scala: Any way to force a trait to return a certain type while permitting any arguments?
- Trigger SBT assembly from other subproject
- Scala implicit object vs implicit val
- Scala traits mixin order and super call