Agent
As we saw in the previous section, GenServer lets you create a server process that can handle synchronous and asynchronous interactions with its clients, while also giving you a way to handle other kinds of low-level messages (sent using Process.send/3) with the handle_info/2 callback.
However, there will be times when you don't want to control whether the communication is sync or asynchronous. You don't need to handle out-of-band messages, you just want to spawn a process that properly stores information in its internal state and shares it when needed. In this case, a GenServer feels overkill for the task at hand.
Instead, you can employ an Agent and get it over with. Think of it as a GenServer tailored to only perform internal state ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access