logo
down
shadow

Either[A, Future[B]] to Future[Either[A, B]]


Either[A, Future[B]] to Future[Either[A, B]]

By : Bradley
Date : November 22 2020, 09:00 AM
wish helps you Not sure there's an out of the box solution, this is what I came up with:
code :
def foldEitherOfFuture[A, B](e: Either[A, Future[B]]): Future[Either[A, B]] =
  e match {
    case Left(s) => Future.successful(Left(s))
    case Right(f) => f.map(Right(_))
  }
case Right(f) => f.map(Right(_)).recover { case _ => Left(/** some error*/) }


Share : facebook icon twitter icon
Blocking code wrapped inside Akka Future also blocks the thread backed by Future, then how is future helpful in this cas

Blocking code wrapped inside Akka Future also blocks the thread backed by Future, then how is future helpful in this cas


By : Dave
Date : March 29 2020, 07:55 AM
Does that help If you only have a single execution context, and you only use futures that execute blocking code, and only one at a time, then there is indeed no advantage from using a future. Advantages of futures are:
Parallelism - on a multicore machine, several threads can execute at the same time. Using futures with an execution context avoids the overhead of creating a new thread for every action or manually reusing threads, and the execution context gives you control over the pool size. Separation of I/O and compute threads, by using different execution contexts. This allows the kernel to schedule interactive responses to I/O ahead of long-running computational tasks, improving latency. True async I/O, via e.g. scala-redis-nb (or Netty or ReactiveMongo(?)). Rather than a Future just being a thread that blocks, it's possible to expose an async API (the kind that might otherwise use callbacks) using Futures (e.g. via Promises), and then you can write truly async code that avoids having any threads blocked on I/O, and with a much nicer API than using a callback-based API directly.
How to flatten a Future in scala - "val _2flat: Future[Option[Future[List[Long]]]]

How to flatten a Future in scala - "val _2flat: Future[Option[Future[List[Long]]]]


By : prakash
Date : March 29 2020, 07:55 AM
Does that help I have this data: , This should do the trick:
code :
val _2BeFlat: Future[Option[Future[List[Long]]]] = _

val flat: Future[Option[List[Long]]] = _2BeFlat.flatMap {
  _ match {
    case None => Future.successful(None)
    case Some(future) => future.map(Some(_))
  }
}
SCALA - expression (for{}yield x) is of type Future[Future[..]] instead of expected Future[..]

SCALA - expression (for{}yield x) is of type Future[Future[..]] instead of expected Future[..]


By : Miroslav Kazimirovic
Date : March 29 2020, 07:55 AM
this will help I have found the answer. The problem was indeed in wrong types (to be exact, with the type of res). It was Future[Seq[(String, Int)]] instead of Seq[(String, Int)] The problem was that I wrapped procFiles (which returns Future[. . .]) into another Future by mistake. Thank's guys for all the comments!
Scala : Better way to handle returning Future from Yeild to avoid future of future

Scala : Better way to handle returning Future from Yeild to avoid future of future


By : Roshan
Date : March 29 2020, 07:55 AM
I wish this helpful for you Consider the below code snippet: , Id put the code inside the for-coprehension:
code :
for {
   substanceTable <- substanceTableF
   mixtureTable <- mixtureTableF 
   result <- {
     if (substanceTable.isEmpty && mixtureTable.isEmpty)
       fallbackResult()
     else
       Future.successful(Some(100))
   }
 } yield result
Scala Future: Transform Future[Vector[T]] to Future[Result]

Scala Future: Transform Future[Vector[T]] to Future[Result]


By : John
Date : March 29 2020, 07:55 AM
To fix the issue you can do The easiest option seems to be splitting it up into map and recover steps:
code :
val result = aCoupleOfFutures
  .map(options => Created(json.Json.toJson(options)))
  .recover{ case exc => BadRequest(exc.getMessage)}
Related Posts Related Posts :
  • Going from local to remote actor messaging in AKKA
  • How to make Mockito verify work with Enumeration
  • Akka model supervision
  • Unit test for Scala object (not class)
  • Getting the element from a 1-element Scala collection
  • Spark - Reduce with division operator
  • SBT - "No Scala version specified or detected" using mirrored repository behind firewall
  • How do you get values for settings from a build.sbt file
  • Play too many arguments for method Apply
  • How to solve transitive dependencies version conflicts (scala/sbt)
  • Writing DataFrame to MemSQL Table in Spark
  • How to add columns into org.apache.spark.sql.Row inside of mapPartitions
  • Set Play Framework Environment
  • store a bunch of configuration information in scala
  • Spark: How to transform LabeledPoint features values from int to 0/1?
  • How to get input from Scala after a certain point in time?
  • Where do I put my tests when I split my Play project into sub modules
  • Read Array in sub queries spark sql using scala
  • Scala map with implicit conversion
  • Spark, Scala, DataFrame: create feature vectors
  • Scala Enumeration: Choose some values as type
  • How can I create a Spark DataFrame from a nested array of struct element?
  • why scala lambda with _ can't using && to combine two bool expression
  • Return elements from array, except for repeating
  • Heroku: deploy Spray based service
  • Mass-add an object if it is an instance of a class
  • Automatically convert a case class to an extensible record in shapeless?
  • How to use pipeTo in AKKA correctly
  • Define a common trait for types with different numbers of type parameters
  • RDD Persistence in Spark
  • Scala trait as a method input - type mismatch error
  • Scala compiler optimization for immutability
  • Base class reference in Scala
  • Akka: The order of responses
  • Why does enablePlugins(DockerPlugin) from sbt-docker in Play project give "error: reference to DockerPlugin is ambi
  • Convert a scala list of Strings into a list of Doubles while discarding unconvertable strings
  • Change the contents of a file in scala
  • Akka Actor testing with ScalaTest using Testkit EventListeners
  • Prepare data for MultilayerPerceptronClassifier in scala
  • Immutability and custom deserialization in Scala
  • Play Framework dependency injection Object vs @Singleton Class
  • Slick 3 transaction how to
  • When should avoid usage of Future in scala
  • Which Spark operation returns the elements not matched by a join?
  • How to group incoming events from infinite stream?
  • Converting a List to a Case Class
  • Get names of the variables in an object
  • Generics re: Scala.math.Ordering wrapper instance for java.lang.Comparable
  • implicit Impl method for collections in scala breeze
  • How to have colored REPL for 'sbt console'?
  • Using the squants library, how do I specify a specific unit of measure for a method?
  • Play Framework: Gzip compression/decompression of data over WebSocket
  • Why does mapPartitions print nothing to stdout?
  • Call doAnswer with function as argument in scala play 2.4
  • Get or create child actor by ID
  • Why substracting two Dates gives me an extra hour?
  • Akka, advices on implementation
  • Spark Scala 2.10 tuple limit
  • Scala currying and type inference
  • Spark DataFrame filtering: retain element belonging to a list
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co