Skip to content

Latest commit

 

History

History
23 lines (18 loc) · 719 Bytes

compute-intermediate-values-in-a-with-construct.md

File metadata and controls

23 lines (18 loc) · 719 Bytes

Compute Intermediate Values In A With Construct

The expressions you use in a with construct do not have to contain the <- syntax. You can pattern match and bind values along the way as well.

with %{id: id} <- get_user(),
     url = "/api/#{id}/blogs",
     %{status_code: 200, body: body} <- HTTPoison.get(url),
     {:ok, decoded_body} <- Poison.decode(body) do
  {:ok, decoded_body}
end

In the above (sorta contrived) example we were able to construct a URL in the middle of the series of expressions.

The values we compute inline will be closed into the with construct, so they won't leak.

See the with docs for more details.