Created
March 2, 2018 09:42
-
-
Save bartschuller/5b0178aeaa82caa69cca8fc42c4e1055 to your computer and use it in GitHub Desktop.
Don't use "if" inside for comprehensions involving Futures thinking they work like sequences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.{ Await, Future } | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
object FutMain { | |
def main(args: Array[String]): Unit = { | |
val part1 = (1 to 3).map(successful) | |
val part2 = Seq(iffed(4)) | |
val part3 = (5 to 7).map(successful) | |
val total = Future.sequence(part1 ++ part2 ++ part3) | |
val res = Await.result(total, 1.minute) | |
println(res) | |
} | |
def successful(i: Int): Future[Int] = Future.successful(i) | |
def iffed(i: Int): Future[Int] = | |
for { | |
testMe <- Future.successful(i) | |
if testMe > 1000 | |
} yield 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will give you