Returning :ok vs :noreply

In the following code, a value is updated if :noreply is returned, however - value is not updated when :ok is returned. Why is this?

  @impl true
  def handle_event("increase", _, socket) do

### this doesn't update value
    {:ok, update(socket, :value, &(&1 + 1))} 

### this DOES update value
    {:noreply, update(socket, :value, &(&1 + 1))} 

  end

Because the Genserver has no receiving function that matches the return with {:ok, something).

It does have one that matches {:noreply, -} and will update the state within that function.

1 Like

I’m pretty sure if you return {:ok, it does more than not update the value, it crashes the whole process!

3 Likes

So much to learn. It’s crazy. Thank you!

There certainly is! Hopefully you enjoy learning. The type specs will guide you.

Spec

handle_event(
  event :: binary(),
  unsigned_params(),
  socket :: Phoenix.LiveView.Socket.t()
) ::
  {:noreply, Phoenix.LiveView.Socket.t()}
  | {:reply, map(), Phoenix.LiveView.Socket.t()}
1 Like

Thank you very much.