The "Await.result
" function in Scala is "blocking", which means that the calling thread will be paused until the awaited Future is completed, at which point it will resume with the returned value.
Pausing a thread can be expensive in a system under high load, as the thread context has to be saved in memory, and it can cause cache misses etc.. Blocking threads is considered poor practice in concurrent programming for that reason.
The async / await
syntax in Javascript is non-blocking. When an async
function invokes "await
", it is converted into a Future, and placed into the execution queue. When the awaited
future is complete, the calling function is marked as ready for execution and it will be resumed at some later point. The important difference is that no Threads need to be paused in this model.
There are a number of libraries which implement the async / await
syntax in Scala, including https://github.com/scala/scala-async
Further reading
- The Futures and Promises book by PRASAD, PATIL, AND MILLER has a good introduction to blocking and non-blocking ops.