我从API获得Future[MyType]
。如何将Future[MyType]
转换为Future[Option[MyType]]
?
def getApiKey(id: String): Future[Option[MyType]] = Future {
val g: Future[Option[MyType]] = getID(id) // error mismatch
g
}
def getID(id: String): Future[MyType] = {
//return Future[MyType]
}
答案 0 :(得分:5)
使用map
:
def getApiKey(id: String): Future[Option[MyType]] = {
getID(id).map(Option(_))
}