I'm trying to use Scala's async
and await
to deal with a future that I am given during a foldLeft
call, so I write a helper function to do the await as I cannot use await
inside a nested function:
我正在尝试使用Scala的异步并等待处理我在foldLeft调用期间给出的未来,所以我写了一个辅助函数来执行await,因为我不能在嵌套函数中使用await:
import scala.await.Await._
def a(f: Future[T]): T = async {
await(f)
}
However the above fails with:
但是上面的失败是:
Error:(33, 38) type mismatch;
found : scala.concurrent.Future[T]
required: T
def a(f: Future[T]): T = async {
^
What am I doing wrong?
我究竟做错了什么?
2 个解决方案
#1
Just like the error says, the return type needs to be Future[T]
. An async block always returns a future.
就像错误所说的那样,返回类型需要是Future [T]。异步块始终返回未来。
Here are the signatures of async and await:
以下是异步和等待的签名:
def async[T](body: T)(implicit execContext: ExecutionContext): Future[T]
def await[T](awaitable: Future[T]): T
You can see that async
is a function of T => Future[T]
. You might notice this is the same signature as the Future { ... }
constructor, AKA Future.apply
. It just constructs a future.
您可以看到async是T => Future [T]的函数。您可能会注意到这与Future {...}构造函数AKA Future.apply的签名相同。它只是构建了一个未来。
The magic is in await
. It pulls the value out of the future so you can write "normal" looking code while still dealing with futures.
魔术正在等待中。它将价值从未来中剔除,因此您可以在处理期货时编写“正常”的代码。
#2
If you want to await the result of an asynchronous computation, instead of using await
in an async
block, you should use Await.result[T]. The async
block is always returning a Future
, and you can use the result of an await
only inside of the async block. Whenever you reuse the awaited value inside the async
block, it's barely equivalent of using the onComplete
method of the underlying Future
.
如果要等待异步计算的结果,而不是在异步块中使用await,则应使用Await.result [T]。异步块始终返回Future,您只能在异步块内部使用await的结果。每当您在异步块中重用等待的值时,它几乎等同于使用底层Future的onComplete方法。
#1
Just like the error says, the return type needs to be Future[T]
. An async block always returns a future.
就像错误所说的那样,返回类型需要是Future [T]。异步块始终返回未来。
Here are the signatures of async and await:
以下是异步和等待的签名:
def async[T](body: T)(implicit execContext: ExecutionContext): Future[T]
def await[T](awaitable: Future[T]): T
You can see that async
is a function of T => Future[T]
. You might notice this is the same signature as the Future { ... }
constructor, AKA Future.apply
. It just constructs a future.
您可以看到async是T => Future [T]的函数。您可能会注意到这与Future {...}构造函数AKA Future.apply的签名相同。它只是构建了一个未来。
The magic is in await
. It pulls the value out of the future so you can write "normal" looking code while still dealing with futures.
魔术正在等待中。它将价值从未来中剔除,因此您可以在处理期货时编写“正常”的代码。
#2
If you want to await the result of an asynchronous computation, instead of using await
in an async
block, you should use Await.result[T]. The async
block is always returning a Future
, and you can use the result of an await
only inside of the async block. Whenever you reuse the awaited value inside the async
block, it's barely equivalent of using the onComplete
method of the underlying Future
.
如果要等待异步计算的结果,而不是在异步块中使用await,则应使用Await.result [T]。异步块始终返回Future,您只能在异步块内部使用await的结果。每当您在异步块中重用等待的值时,它几乎等同于使用底层Future的onComplete方法。