So here is my code :
defmodule Test do
def fun() do
Process.sleep(10000)
IO.puts "sleep over"
end
def dummy(:b) do
spawn(fun())
end
def dummy(:a) do
spawn(Test,:fun,[])
end
def dummy() do
spawn(fn -> Process.sleep(10000)
IO.puts "sleep over"
end)
IO.puts "process started"
end
end
On running this code and executing various dummy functions I got this output:
iex(1)> c("test.exs")
[Test]
iex(2)> Test.dummy
process started
:ok
sleep over
iex(3)> Test.dummy :
** (SyntaxError) iex:3: unexpected token: ":" (column 12, codepoint U+003A)
iex(3)> Test.dummy :a
#PID<0.111.0>
iex(4)>
nil
iex(5)>
nil
sleep over
iex(6)> Test.dummy :b
sleep over
** (ArgumentError) argument error
:erlang.spawn(:ok)
iex(6)>
My main concern is why when we use spawn/1 with not an anonymous function it does not asynchronously execute the function but wait for this function to execute while in both other the functions inside spawn are executed asynchronously (as I expected) .