...

/

Parameters of the async_stream Function

Parameters of the async_stream Function

Learn about the parameters of the async_stream function and how they work.

Now, let’s update notify_all/1 to use async_stream/3. However, this time, we run the stream using Enum.to_list/1:

Press + to interact
#file path -> sender/lib/sender.ex
def notify_all(emails) do
emails
|> Task.async_stream(&send_email/1)
|> Enum.to_list()
end

Here is the executable command:

Press + to interact
Sender.notify_all(emails)

This is the output we get:

iex(1)> Sender.notify_all(emails)
Email to hello@world.com sent
Email to hola@world.com sent
Email to nihao@world.com sent
Email to konnichiwa@world.com sent
[
  ok: {:ok, "email_sent"},
  ok: {:ok, "email_sent"},
  ok: {:ok, "email_sent"},
  ok: {:ok, "email_sent"}
]

We make changes in the playground widget at the end and click the “Run” button to execute the code. The output will be similar to the Task.async/2 example. However, depending on how many logical cores our machine ...