What is the difference of Future/Await and Async/Await
Asked Answered
C

2

5

In Scala and other programming languages one can use Futures and Await.

(In real code one would use e.g. zip+map instead of Await)

def b1() = Future { 1 }
def b2() = Future { 2 }

def a() = Future {
  Await.result(b1(),Duration.inf) + Await.result(b2(),Duration.inf)
}

What is the difference to Async/Await in Javascript/Scala?

async function b1() { return 1 }
async function b2() { return 3 }

async function a() { 
  return await b1() + await b2()
}
Counterespionage answered 23/4, 2018 at 8:25 Comment(0)
C
6

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.
Corie answered 23/4, 2018 at 8:51 Comment(7)
Thanks @Rich, does the Scala asyn/await library work the same way but putting Futures in the queue and resuming the function? asyn/await is blocking the execution of the function but not occupying a thread by blocking it, if I understand you right. async/await works more like goroutines in Go or green threads?Counterespionage
The main reason to avoid blocking is the danger of deadlock; performance is less of an issue.Mencius
@Tim, I don't think that's true. Perhaps you are thinking of "non blocking" in the sense of lock-free contention resolution algorithms? I think that is quite different from "non-blocking" in the sense under discussion here. See en.wikipedia.org/wiki/Non-blocking_algorithm and the "Not to be confused with" link. I believe that async/await code has exactly the same deadlock properties as synchronous code, assuming that you translate the exclusive locks. In any case, the motivation for async code is tangential to this question. Sorry if I confused matters by adding it to my answer.Corie
@StephanSchmidt, yes, the Scala async/await library works in an analogous way to async/await in Javascript. I think I know what you are getting at with the "green threads" comment, but Green v.s. native threads are actually orthogonal to the programming syntax distinction between Futures and async/await. This comment box is too small for that discussion though :-) See en.wikipedia.org/wiki/Green_threadsCorie
@Corie Thanks for those explainations!Counterespionage
@Corie Don't worry, I am not confused. The main reason to use async futures/callbacks rather than blocking threads is to avoid deadlock. (Remember Mars Pathfinder...)Mencius
@Tim: This is getting off-topic, but for the record, 1) async programming does not avoid deadlocks or reduce them when compared to threaded/blocking programming. See e.g. google.co.uk/search?q=do+futures+avoid+deadlocks 2) the motivation for async I/O is performance, not deadlocks, see e.g. gigi.nullneuron.net/gigilabs/motivation-for-async-await-in-c or engblog.yext.com/post/why-async 3) The Mars Pathfinder had a scheduling problem, not a deadlock problem. See e.g. robotnews.wordpress.com/2007/04/03/…Corie
G
1

Await is blocking while async is non blocking. Await waits in the current thread to finish the task while async won't block the current thread and runs in background.

I don't have idea about JavaScript. In Scala Await from scala.concurrent package is used for blocking main thread. There's another library called scala-async what is used for using await inside async block.

If you are using scala-async, then you need to call await inside async

Goddamn answered 23/4, 2018 at 8:28 Comment(1)
The await b1() should be blocking, inside a thread run by async function a(), shouldn't it? Is Javascript using green threads e.g. in Node here?Counterespionage

© 2022 - 2024 — McMap. All rights reserved.