Q:

Async await assignment

I was looking over some Scala server code and I saw thins async/await block:
async {
  while (cancellationToken.nonCancelled) {
    val (request, exchange) = await(listener.nextRequest)
    respond(exchange, cancellationToken, handler(request))
  }
}

How can this be correct syntax?
As I understand it:
For every execution of the while loop

Thread 1 will execute the code from the while loop except the one in the await clause.
Thread 2 will go in the await clause.

But then Thread 1 will have val (request, exchange) uninstantiated in case Thread 2 doesn't finish computing.
These values will be passed to the respond and handler methods uninstantiated.
So how can you have an assignment in two different threads?

A:

So how can you have an assignment in two different threads?

async-await's main goal is to allow you to do asynchronous programming in a synchronous fashion.
What really happens is that the awaited call executes listener.nextRequest and asynchronously waits for it's completion, it doesn't execute the next line of code until then. This guarantees that if the next line of code is executed, it's values are populated. The assignment should happen where it is visible to the next LOC in the method.
This is possible due to the fact that the async macro actually transforms this code into a state-machine, where the first part is the execution up until the first await, and the next part is everything after.

