Search code examples
scalaakkaakka-stream

Akka Stream: Which one is better - scan or scanAsync?


I am a newbie to Akka Stream, so apology, if I ask any stupid question.

I was going through the Akka Stream QuickStart guide. And I found scan operation. The documentation says that it is similar to fold but is not a terminal operation. I got that part, but when I found scanAsync then the documentation said the same thing, except for one thing, i.e., it is similar to scan but with an asynchronous function.

Now, my doubts are:

  1. What is the difference between them? and,
  2. Which one is the better one to use?

Solution

    1. The main difference between them is that result of the given function f is different. The output value of the f function in the case with scanAsync is Future. So the next computation will begin as soon as the Future will be completed. But, when using scan computation of the next value will begin immediately after the computing of the current one.
    2. General answer: it depends. However here is some advice: You should use scan when computation of the next value depends only on CPU and there is no temporary disk/network read/writes, and use scanAsync otherwise

    Hope it helped!!!