OkThen.Result.then

You're seeing just the function then, go back to OkThen.Result module for more information.
Link to this function

then(result, func_or_value)

Specs

then(result_input(), func_or_value(out)) :: out when out: any()

If result is tagged :ok, passes the wrapped value into func_or_value and returns the result. If a function is not provided, the argument at the same position is returned as-is.

If result is not tagged with the specified tag atom, result is returned as-is.

Use this function to pipe results into functions that return tagged tuples.

Be aware that no attempt is made to ensure the return value from the function is a tagged tuple. However, all functions are tolerant of untagged results, and on input will interpret them as an {:untagged, value} tuple.

Equivalent to tagged_then(result, :ok, func_or_value). See tagged_then/3.

Examples

iex> :ok |> Result.then({:ok, "hello"})
{:ok, "hello"}

iex> {:ok, 1} |> Result.then({:ok, "hello"})
{:ok, "hello"}

iex> :none |> Result.then({:ok, "hello"})
:none

iex> :ok |> Result.then(fn {} -> {:ok, "hello"} end)
{:ok, "hello"}

iex> {:ok, 1} |> Result.then(fn 1 -> {:ok, "hello"} end)
{:ok, "hello"}

iex> {:ok, 1, 2} |> Result.then(fn {1, 2} -> {:ok, "hello"} end)
{:ok, "hello"}

iex> {:ok, 1, 2} |> Result.then(fn 1, 2 -> {:ok, "hello"} end)
** (ArgumentError) Value-mapping function must have arity between 0 and 1.

iex> {:ok, 1, 2} |> Result.then(fn {1, 2} -> :ok end)
:ok

iex> :error |> Result.then(fn _ -> {:ok, "hello"} end)
:error

iex> {:error, 1} |> Result.then(fn _ -> {:ok, "hello"} end)
{:error, 1}

iex> {:error, 1, 2} |> Result.then(fn _ -> {:ok, "hello"} end)
{:error, 1, 2}

iex> :none |> Result.then(fn _ -> {:ok, "hello"} end)
:none

iex> :something_else |> Result.then(fn _ -> {:ok, "hello"} end)
:something_else

iex> "bare value" |> Result.then({:ok, "hello"})
"bare value"

iex> "bare value" |> Result.then(fn _ -> {:ok, "hello"} end)
"bare value"