scala - 值 onSuccess 不是 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("使用
foreach
oronComplete
代替(请记住,它们采用全部而不是部分功能)", "2.12.0")
你最好的朋友onComplete
现在
f.onComplete {
case Success(value) => ???
case Error(ex) => ???
}
推荐阅读
- salesforce - 如何在 Salesforce 中处理 HOLD 项目?
- dji-sdk - 是否可以从防撞摄像头(DJI 的立体摄像头)访问实时视频源?
- sql-server - 如何在 Sql server 中为两组用户 - 管理员和客户创建用户登录和身份验证?
- c - 带有结构的 EXC_BAD_ACCESS
- javascript - 使用getElementById()提交后如何在下一页上显示具有多种类型值的html表单?
- jasper-reports - 如何使用 jasper studio 在 jasper 报表中添加动态图像
- gradle - 从 Jenkinsfile 中的不同 git repo 运行任务
- css - Element is not working for transitions in css3
- asp.net-core - AddDbContext 在 1 个请求中创建 2 个实例
- javascript - 如何使用 Flow 导出动作创建者返回值的类型?