Search code examples
elixirgen-server

Elixir GenServer handle_call callback return type


I am implementing a GenServer and I am confused.

I know handle_cast is asynchronous, which means the caller is not waiting for a reply and we return a tuple like this: {:noreply, new_state}.

I have noticed that we can also return the same tuple from handle_call. Does it mean if I have a handle_call that returns {:noreply, new_state}, it wont return anything but will be synchronous? The flow of the caller will wait on the GenServer.call command, and then continue after the handle_call function has completed?


Solution

  • The caller will block waiting for a reply whenever you invoke GenServer.call. The reason for handle_call to accept {:noreply, state} as a valid return is to allow you to reply/2 manually (from any place).

    The calling process will block waiting for a reply (and timeout according to your settings, etc), just the same.

    The second argument to the handle_callback is a from reference, as in handle_call(msg, from, state), that can then be used with reply/2.