I have two methods:
def getNextJob: Future[Option[Job]]
def process(job: Job): Future[Unit]
I would like to process all Jobs until there are no jobs remaining.
I can do this with Await
e.g.
private def process()(implicit ctx: ExecutionContext): Future[Unit] = {
var job: Option[Job] = Await.result(service.getNextJob, FiniteDuration(2, TimeUnit.SECONDS))
while(job.isDefined) {
Await.result(process(job.get), FiniteDuration(2, TimeUnit.SECONDS))
job = Await.result(service.getNextJob, FiniteDuration(2, TimeUnit.SECONDS))
}
Future.successful()
}
But this is ugly and doesn't use Futures properly. Is there a way I could chain the futures somehow to replace this?