Skip to content

Instantly share code, notes, and snippets.

@clone1018
Last active November 16, 2018 19:43
Show Gist options
  • Save clone1018/0295f04daaf7aed54cefe4b111a4e942 to your computer and use it in GitHub Desktop.
Save clone1018/0295f04daaf7aed54cefe4b111a4e942 to your computer and use it in GitHub Desktop.
private def processBatch(results: Future[Seq[String]], batch: Seq[String]): Future[Seq[String]] = {
// when the results for the previous batches have been completed, start a new batch
results.flatMap { responses =>
// create the web requests for this batch
val batchFutures: Seq[Future[String]] = batch.map(ws.url(_).get().map(_.body))
// sequence the futures for this batch into a singe future
val batchFuture: Future[Seq[String]] = Future.sequence(batchFutures)
// when this batch is complete, append the responses to the existing responses
batchFuture.map { batchResponses =>
Logger.info("Finished a batch")
responses ++ batchResponses
}
}
}
private def processBatch(results: Future[Seq[String]], batch: Seq[String]): Future[Seq[String]] = {
// when the results for the previous batches have been completed, start a new batch
for {
responses <- results
// sequence the futures for this batch into a singe future
batchFuture = Future.sequence(batch.map(ws.url(_).get().map(_.body)))
batchResponses <- batchFuture
} yield {
// when this batch is complete, append the responses to the existing responses
Logger.info("Finished a batch")
responses ++ batchResponses
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment