Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Future[String]转换为String scala?_Scala - Fatal编程技术网

如何将Future[String]转换为String scala?

如何将Future[String]转换为String scala?,scala,Scala,我有一个函数,它接受字符串parametr def externalServerErrorresponse:String,我无法发送我未来的[String]parametr 我试图将一个函数更改为def externalServerErrorresponse:Future[String],但它给出了一个错误,因为在内部,我使用库ServerErrorRequestException中的类,该类需要选项[String]。以下是完整代码: def externalServerError(respo

我有一个函数,它接受字符串parametr def externalServerErrorresponse:String,我无法发送我未来的[String]parametr 我试图将一个函数更改为def externalServerErrorresponse:Future[String],但它给出了一个错误,因为在内部,我使用库ServerErrorRequestException中的类,该类需要选项[String]。以下是完整代码:

 def externalServerError(response: String): ErrorInfo = {
    val apiException = ServerErrorRequestException(
      message = Some(response)
    )
    apiExceptionToErrorInfo(apiException)
  }

}

您可以完全切换到异步:

或者使用被认为不好的样式

def externalServerError(response: Future[String]): Future[ErrorInfo] = {
    response.map(str =>
       apiExceptionToErrorInfo(
          ServerErrorRequestException(
            message = Some(str)
         )
       )
    )
  } 
}
def externalServerError(response: Future[String]): Future[ErrorInfo] = {
    val str = Await.result(response, 1.second)
    apiExceptionToErrorInfo(
       ServerErrorRequestException(
         message = Some(str)
       )
    )
  } 
}