Agent.start_link
start_link, go back to Agent module for more information.
Specs
start_link((() -> term()), GenServer.options()) :: on_start()
Starts an agent linked to the current process with the given function.
This is often used to start the agent as part of a supervision tree.
Once the agent is spawned, the given function fun is invoked in the server
process, and should return the initial agent state. Note that start_link/2
does not return until the given function has returned.
Options
The :name option is used for registration as described in the module
documentation.
If the :timeout option is present, the agent is allowed to spend at most
the given number of milliseconds on initialization or it will be terminated
and the start function will return {:error, :timeout}.
If the :debug option is present, the corresponding function in the
:sys module will be invoked.
If the :spawn_opt option is present, its value will be passed as options
to the underlying process as in Process.spawn/4.
Return values
If the server is successfully created and initialized, the function returns
{:ok, pid}, where pid is the PID of the server. If an agent with the
specified name already exists, the function returns
{:error, {:already_started, pid}} with the PID of that process.
If the given function callback fails, the function returns {:error, reason}.
Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.get(pid, fn state -> state end)
42
iex> {:error, {exception, _stacktrace}} = Agent.start(fn -> raise "oops" end)
iex> exception
%RuntimeError{message: "oops"}
Specs
start_link(module(), atom(), [any()], GenServer.options()) :: on_start()
Starts an agent linked to the current process.
Same as start_link/2 but a module, function, and arguments are expected
instead of an anonymous function; fun in module will be called with the
given arguments args to initialize the state.