Skip to content

Instantly share code, notes, and snippets.

@dokkaebi
Created April 11, 2018 19:05
Show Gist options
  • Save dokkaebi/25dfed8d30bdecf8db8eb0269dd2705c to your computer and use it in GitHub Desktop.
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
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
Copy link

entrust_site_seal_small

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