Created
April 11, 2018 19:05
-
-
Save dokkaebi/25dfed8d30bdecf8db8eb0269dd2705c to your computer and use it in GitHub Desktop.
Scala - arbitrary length chained futures where each depends on the result of the previous
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
def pieceOfWork(i: Int): Int = new Random().nextInt(10) + i | |
def eventualWork(i: Int): Future[Int] = Future { | |
println(s"eventualWork $i") | |
Thread.sleep(500) | |
val r = pieceOfWork(i) | |
println(s"returning $r") | |
r | |
} | |
def findMoreThan(total: Int): Future[Int] = { | |
val promise = Promise[Int]() | |
def r(i: Int): Unit = { | |
println("r") | |
eventualWork(i).map(t => { | |
if (t > total) promise.success(t) | |
else r(t) | |
}) | |
} | |
r(0) | |
promise.future | |
} |
forever113
commented
Apr 11, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment