もう一つのオプションは再帰関数です。 foldLeft
より簡潔ではありますが、一部ではより読みやすくなります。ちょうどあなたの検討のためのもう一つの選択肢は(save(entity: T): Future[R]
を前提としています。あなたのsave
メソッドが許可されている場合
は
def saveAll(entities: List[T]): Future[List[R]] = {
entities.headOption match {
case Some(entity) =>
for {
head <- save(entity)
tail <- saveAll(entities.tail)
} yield {
head :: tail
}
case None =>
Future.successful(Nil)
}
}
さらに別のオプションは、あなたがあなた自身のExecutionContext
すなわちsave(entity: T)(implicit ec: ExecutionContext): Future[R]
、ちょうど同時にFuture
秒を発射された供給が、シングルスレッド実行コンテキストを使用します:??
def saveAll(entities: List[T]): Future[List[R]] = {
implicit ec = ExecutionContext.fromExecutionService(java.util.concurrent.Executors.newSingleThreadExecutor)
Future.sequence(entities.map(save))
}
あなたが開始されない場合、なぜあなたは挿入文をループで一つ一つを実行していないデータベースに挿入する非同期メソッドを持っていますか – ovunccetin