Agent.get_and_update
get_and_update
, go back to Agent module for more information.
Specs
Gets and updates the agent state in one operation via the given anonymous function.
The function fun
is sent to the agent
which invokes the function
passing the agent state. The function must return a tuple with two
elements, the first being the value to return (that is, the "get" value)
and the second one being the new state of the agent.
timeout
is an integer greater than zero which specifies how many
milliseconds are allowed before the agent executes the function and returns
the result value, or the atom :infinity
to wait indefinitely. If no result
is received within the specified time, the function call fails and the caller
exits.
Examples
iex> {:ok, pid} = Agent.start_link(fn -> 42 end)
iex> Agent.get_and_update(pid, fn state -> {state, state + 1} end)
42
iex> Agent.get(pid, fn state -> state end)
43
Specs
Gets and updates the agent state in one operation via the given function.
Same as get_and_update/3
but a module, function, and arguments are expected
instead of an anonymous function. The state is added as first
argument to the given list of arguments.