Skip to content

Instantly share code, notes, and snippets.

@olix0r
Created May 5, 2016 04:58
Show Gist options
  • Save olix0r/22257b1234354eea2a567495e9826798 to your computer and use it in GitHub Desktop.
Save olix0r/22257b1234354eea2a567495e9826798 to your computer and use it in GitHub Desktop.
/*
* check a condition asynchronously until it's true and then do another thing
*/
def checkCond: Future[Boolean] = ???
def doThing[T]: Future[T] = ???
implicit val timer = DefaultTimer.twitter
def untilCondition(backoff: Stream[Duration]): Future[Unit] =
checkCond.flatMap {
case false =>
backoff match {
case wait #:: backoff =>
Future.sleep(wait).before(untilCondition(backoff))
case _ => Future.exception(new Exception("I fucked up"))
}
case true => Future.Unit
}
val thing: Future[T] = untilCondition(Backoff.exponentialJittered(0.millis, 1.minute)).before(doThing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment