Skip to content

Instantly share code, notes, and snippets.

@alvises
Created March 18, 2019 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvises/32f8c76bc14dca590b35deec2c47c04c to your computer and use it in GitHub Desktop.
Save alvises/32f8c76bc14dca590b35deec2c47c04c to your computer and use it in GitHub Desktop.
async callback
defmodule Example do
use GenServer
def start_link(_) do
GenServer.start_link __MODULE__, :ok, []
end
def init(:ok) do
{:ok, []}
end
def add(pid,{first_name,last_name}) do
GenServer.cast(pid,{:add, first_name, last_name})
IO.puts("after calling add")
end
def handle_cast({:add, first_name, last_name},state) do
task = Task.async(fn ->
:timer.sleep(5_000)
Enum.join([first_name,last_name]," ")
end)
{:noreply, state}
end
def handle_info({_ref, first_and_last_name}=msg,state) do
IO.inspect(msg, label: "handle_info")
{:noreply, [first_and_last_name | state]}
end
def handle_info(_, state), do: {:noreply, state}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment