Quick look at new :gen_server handle_continue callback

Tobiasz Małecki
Jun 20, 2018 · 2 min read

19 June 2018. Erlang OTP 21.0 has been released today and I’m going to describe how new :gen_server callback works.


Erlang and Elixir installed using asdf version manager

Let’s write something basic

Initial state of our GenServer is 0 and it implements single that increments state by 1 and logs new value.

Inside init callback we are sending three messages to GenServer itself and logging additional message, so when we start it we can see following logs:

Nothing special so far.


Let’s modify return value and add new callback.

When we start our GenServer now, we can see different values in logs:

The reason for this is that when any callback returns then is invoked immediately. In other words after every there was which incremented state value one more time.

More realistic use case

Let’s imagine that returns current state and calculates new one.

Process invoking call doesn’t need to know new state, but it’s blocked until calculation ends.
It also can’t be solved by moving calculation to callback because it can’t be guaranteed that there are no other messages waiting for being processed.

It’s a kind of situation where is very convenient:

Last thing

There's no support for in elixir yet. It means that before will result in compilation warning.

AppUnite collection

We teach how to write apps

Thanks to AppUnite.

Tobiasz Małecki

Written by

Backend Web Developer, OpenSource contributor, creator of “mockery”, “conductor” and “beam_inspect” elixir libraries.

AppUnite collection

We teach how to write apps