import scala.concurrent.Future
      
      def predicate(condition: Boolean)(fail: Exception): Future[Unit] = 
          if (condition) Future( () ) else Future.failed(fail)
      
      Scala language logo

      Scala Future with predicate in for comprehension

      comprehension
      predicate
      future
      ...

      Idiomatic solution to have filter, conditionals, predicates in Futures' for-comprehension

      Example of usage:

      val f2 = for {
        i <- f1
        _ <- predicate( i == 2 )(new Exception("Test2"))
        j <- f3  // f3 will only run if the predicate is true
      } yield "Test1"
      

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.