值 onSuccess 不是 scala.concurrent.Future[Any] 的成员

value onSuccess is not a member of scala.concurrent.Future[Any]

我想从 URL 发出请求,但遇到了问题:

val f: Future[Any] = actor1 ? SyncRequest(url)
  f.onSuccess {
    case feed: xml.Elem => 
      val feedInfo = FeedInfo(
              ((feed \ "channel") \ "title").headOption.map(_.text).get,
              ((feed \ "channel") \ "description").headOption.map(_.text),
              ((feed \ "channel") \ "item").map(item =>
                FeedItem((item \ "title").headOption.map(_.text).get,
                          (item \ "link").headOption.map(_.text))
              ).toList
            )

      complete(feedInfo)

这是错误:

[error] value onSuccess is not a member of scala.concurrent.Future[Any]
[error]                 f.onSuccess {
[error]                     ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

也许我必须使用 OnComplete 而不是 OnSuccess

onSuccess 方法在 Scala 2.12 中已弃用并在 Scala 2.13 中删除

@deprecated("use foreach or onComplete instead (keep in mind that they take total rather than partial functions)", "2.12.0")

你最好的朋友现在是 onComplete

f.onComplete {
  case Success(value) => ???
  case Error(ex) => ???
}