首页 > 解决方案 > 如何在不阻塞的情况下运行一系列期货并等待完成?

问题描述

我在 Scala 中要求运行一系列必须按顺序完成且无阻塞的 http 调用。我怎样才能做到这一点?

标签: scalafuture

解决方案


您想查看foldLeft函数或TraversableLike包含一系列调用或其规范:

val seriesOfOrderedCalls = Seq(..)
val eventuallyCompleted = seriesOfOrderedCalls
  .foldLeft(Future.successful(()))((prev, call) => {
    prev.flatMap { _ =>
      // do your call here
      // then return the future of the call
      Future.successful(())
    }
  })

推荐阅读