Scala类型不匹配:找到Future [MyType]需要Future [Option [MyType]]

时间:2018-05-01 15:13:45

标签: scala

我从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]
}

1 个答案:

答案 0 :(得分:5)

使用map

def getApiKey(id: String): Future[Option[MyType]] = {
  getID(id).map(Option(_))
}