我在Actor中有以下代码
def receive = {
case All() => {
val collection: BSONCollection = db("ping")
val future:Future[List[Ping]] = collection.find(BSONDocument()).cursor[Ping].toList()
val zender = sender
future onComplete {
case Success(list) => zender ! list
case Failure(throwable) => zender ! List()
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢如何使用onComplete函数将结果发送回发送方actor.我想知道是否可以将它转换成这样的东西:
def receive = {
case All() => {
val collection: BSONCollection = db("ping")
val future:Future[List[Ping]] = collection.find(BSONDocument()).cursor[Ping].toList()
"sender ! future" // one option
"future.map( list => sender ! list)" //Another option. I know it's not map, but maybe another function
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得未来的链接会更好地流动.
| 归档时间: |
|
| 查看次数: |
7300 次 |
| 最近记录: |