Skip to content

Instantly share code, notes, and snippets.

@bartschuller
Created March 2, 2018 09:42
Show Gist options
  • Save bartschuller/5b0178aeaa82caa69cca8fc42c4e1055 to your computer and use it in GitHub Desktop.
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
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
}
@bartschuller
Copy link
Author

This will give you

[error] (run-main-4) java.util.NoSuchElementException: Future.filter predicate is not satisfied
java.util.NoSuchElementException: Future.filter predicate is not satisfied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment