Using Task.async to apply concurrency
Achieving concurrency is pretty natural in Elixir. Here's a list of urls for which we want to make a GET
request using HTTPoison:
urls
|> Enum.map(&HTTPoison.get/1)
The above will code works fine, but it is synchronous. To make it asynchronous we need to change just one line of code:
urls
|> Task.async_stream(&HTTPoison.get/1)
|> Enum.into([], fn {_key, value} -> value end)
Member discussion