简体   繁体   English

如何将 Future[Either[Throwable,T]] 展开为 Either[Throwable,T]

[英]How to unwrap a Future[Either[Throwable,T ]] to Either[Throwable,T]

I have a function ( myFunc ) in scala that gives Future[Either[Throwable,T ]] .我在scala中有一个functionmyFunc ),它给出Future[Either[Throwable,T ]] Now I need to unwrap and get Either[Throwable,T ] out of it and pass to as an input parameter to another function ( anotherFunc ).现在我需要打开并从中取出Either[Throwable,T ]并将其作为输入参数传递给另一个 function ( anotherFunc )。

def myFunc(input: String): Future[Either[Throwable, HttpResponse]] = {
  ....
}

def anotherFunc(response: Either[Throwable, T]) # signature

anotherFunc(myFunc("some string"))

Normally we use map to transform a Future but thats not helping me here通常我们使用map来改变Future ,但这对我没有帮助

myFunc("some string").map { _ =>
            anotherFunc(_)
          }

This causes problem with the return of the block from where I am calling.这会导致从我调用的块return问题。

You can't unwrap the value of a Future because a Future represents the result of an asynchronous computation that may or may not be available yet.您无法解开Future的值,因为Future表示异步计算的结果,该结果可能还可能不可用 By default, futures and non-blocking, encouraging the use of callbacks instead of typical blocking operations.默认情况下,futures 和非阻塞,鼓励使用回调而不是典型的阻塞操作。

What you can do is either:你可以做的是:

  • use combinators such as map , flatMap , filter to compose futures in a non-blocking way.使用mapflatMapfilter等组合器以非阻塞方式组合期货。
  • register a callback using the onComplete method, or foreach if you want to call a callback only when the Future completes successfully.使用onComplete方法注册一个回调,如果您只想在Future成功完成时调用回调,则使用foreach
  • block the main thread using Await.result , if this is absolutely necessary, although is discouraged.使用Await.result阻塞主线程,如果这是绝对必要的,虽然不鼓励。 If you want to transform the Future result or combine it with others, you should opt for the 2 previos non-blocking ways mentioned.如果您想转换Future结果或将其与其他结果结合,您应该选择前面提到的 2 种非阻塞方式。

That being said.话虽如此。 These are the preferred approaches:这些是首选方法:

  def anotherFunc[T](response: Future[Either[Throwable, T]]) = {
    response.onComplete {
      case Failure(exception) => // process exception
      case Success(value)     => // process value
    }
  }

  def anotherFunc2[T](response: Future[Either[Throwable, T]]) = {
    response.map {
      case Left(exception) => // process exception
      case Right(value)    => // process value
    }
  }

Then you can do:然后你可以这样做:

  anotherFunc(myFunc("some string"))
  anotherFunc2(myFunc("some string"))

NOTE : If you can't change the signature of anotherFunc[T](response: Either[Throwable, T]) then just do:注意:如果您无法更改anotherFunc[T](response: Either[Throwable, T])的签名,那么只需执行以下操作:

  myFunc("some string").map(anotherFunc)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从几个未来[\ / [Throwable,T]]获得未来[\ / [Throwable,T]] - How to get Future[\/[Throwable, T]] from several Future[\/[Throwable, T]] 是否可以将输出为Either [Throwable,Unit]的同步函数调用转换为Future [Either [Throwable,Unit]]? - Is it possible to convert synchronous function call with output of Either[Throwable, Unit] to Future[Either[Throwable, Unit]]? ZIO 在 Future[Either[MyException, A]] 中传播自定义异常系列,没有仅可抛出的任务 - ZIO Propagate custom exception family within Future[Either[MyException, A]], without Task which is Throwable only 未来[A]与未来[Throwable \\ / A] - Future[A] vs Future[Throwable \/ A] 使用捕捉(...)时,我可以避免冗余地施放Throwable吗? - Can I avoid redundantly casting a Throwable when using catching(…).either? Scala-是否有映射Seq [A] => Seq [Either [Throwable,B]]的函数? - Scala - Is there a function to map Seq[A] => Seq[Either[Throwable, B]]? scalatra case t:Throwable => t.printStacktrace() - scalatra case t: Throwable => t.printStacktrace() 如何将Throwable \\ / List [Throwable \\ / A]序列化为scalaz中的Throwable \\ / List [A]? - How to sequence Throwable \/ List[Throwable \/ A] into Throwable \/ List[A] in scalaz? 如何将[未来[A],未来[B]]转换为未来[[A,B]] - How to transform Either[Future[A], Future[B]] to Future[Either[A, B]] 结合`OptionT`和`EitherT`来处理`Future [[Error,Option [T]]]` - Combining `OptionT` and `EitherT` to handle `Future[Either[Error, Option[T]]]`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM