Scala 执行包含在主要期货中的惰性期货的便捷方式

Scala 执行包含在主要期货中的惰性期货的便捷方式,scala,Scala,有以下情况: type LazyFuture = () => Future val mainFuture: Future[Seq[LazyFuture]] = Future { ... // some actions which can throw exceptions ... // inner lazy futures as result of main future // they has to be executed only if the

有以下情况:

type LazyFuture = () => Future

val mainFuture: Future[Seq[LazyFuture]] = Future {
    ...
    // some actions which can throw exceptions
    ...

    // inner lazy futures as result of main future
    // they has to be executed only if the main future is completed successfully
    val xs: Seq[LazyFuture] = Seq(
      () => Future(1),
      () => Future(2),
      ...
      () => Future(N)
    )
    xs
}
如果
mainfurture
成功执行,是否有方便的方法执行内部延迟期货。以下内容说明了该任务:

val resultFuture:Future[Any] = 
      if(mainFuture is success) 
        // exec inner lazy futures
        Future.sequence(mainFuture.result.map(c => c())) 
      else
        // nothing
        Future.successful(Unit) 

我为上述任务找到了以下解决方案

type LazyFuture = () => Future

val mainFuture: Future[Seq[LazyFuture]] = Future {

    val xs: Seq[LazyFuture] = Seq(
      () => Future(1),
      () => Future(2),
      ...
      () => Future(N)
    )
    xs
}

val promise = Promise[Future[Seq[Any]]]()

mainFuture.onComplete {
  case Success(xs: Seq[LazyFuture]) =>
    val eventualSeq: Future[Seq[Any]] = Future.sequence(xs.map(c => c()))
    promise success eventualSeq
  case Failure(t) => promise failure t
}

val resultFuture: Future[Any] = promise.future.flatten

此解决方案足够好吗?可能会出现任何问题吗?

我为上述任务找到了以下解决方案

type LazyFuture = () => Future

val mainFuture: Future[Seq[LazyFuture]] = Future {

    val xs: Seq[LazyFuture] = Seq(
      () => Future(1),
      () => Future(2),
      ...
      () => Future(N)
    )
    xs
}

val promise = Promise[Future[Seq[Any]]]()

mainFuture.onComplete {
  case Success(xs: Seq[LazyFuture]) =>
    val eventualSeq: Future[Seq[Any]] = Future.sequence(xs.map(c => c()))
    promise success eventualSeq
  case Failure(t) => promise failure t
}

val resultFuture: Future[Any] = promise.future.flatten
这个解决方案是否足够好,是否会出现任何问题