SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. defmodule Example do
  2.   use GenServer
  3.  
  4.   def start_link(_) do
  5.     GenServer.start_link __MODULE__, :ok, []
  6.   end
  7.  
  8.   def init(:ok) do
  9.     {:ok, []}
  10.   end
  11.  
  12.   def add(pid,{first_name,last_name}) do
  13.     GenServer.cast(pid,{:add, first_name, last_name})
  14.     IO.puts("after calling add")
  15.   end
  16.  
  17.   def handle_cast({:add, first_name, last_name},state) do
  18.     task = Task.async(fn ->
  19.       :timer.sleep(5_000)
  20.       Enum.join([first_name,last_name]," ")
  21.     end)
  22.     {:noreply, state}
  23.   end
  24.  
  25.   def handle_info({_ref, first_and_last_name}=msg,state) do
  26.     IO.inspect(msg, label: "handle_info")
  27.     {:noreply, [first_and_last_name | state]}
  28.   end
  29.  
  30.   def handle_info(_, state), do: {:noreply, state}
  31. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top