Reputation: 15345
In the following sample method implementation that uses the Scala async:
def doSomething: Future[Int] = async {
val await1 = await(someFutureResult1()) // Line 1
val await2 = await(someFutureResult2()) // Line 2
val syncCall = someFunc3() // Line 3
await1 + await2 + syncCall // Line 4
}
How is the ordering happening? The caller of doSomething returns immediately because the doSomething() is going to run in an async block. I get that. But what happens when some other thread picks up this execution and executes the contents of doSomething?
My understanding is as follows:
Line 1 starts and waits until the Future returned by someFutureResult1() is completed. Line 2 and Line 3 is not yet started!
Line 2 starts and waits until the Future returned by someFutureResult2() is completed
Line 3 starts and returns
Line 4 is called and returned
Is the flow correct?
Upvotes: 0
Views: 89
Reputation: 11498
It depends what you meant by "waits", what happens is that, unlike with Scala's ordinary Await.result
which blocks the thread, await
wraps the rest of the code as a Future
, and frees up the thread to continue running outside the async
block.
Upvotes: 1