Created
March 18, 2019 13:17
-
-
Save alvises/32f8c76bc14dca590b35deec2c47c04c to your computer and use it in GitHub Desktop.
async callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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