We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Running Task.async_stream/3 for side effects
almirsarajcic
Calling Task.async_stream/3 does not start any tasks:
Task.async_stream(customer_ids, &Mailer.deliver_invoice/1)
It returns a lazy stream. The tasks start only when something enumerates that stream, so ignoring the return value silently skips every delivery.
If you only need the side effects, run the entire stream:
customer_ids
|> Task.async_stream(&Mailer.deliver_invoice/1, ordered: false)
|> Stream.run()
Use Enum.to_list/1 or another Enum function when you need the {:ok, value} results. Adding another Stream step does not start the work, because the pipeline is still lazy.
Mind you, stopping consumption early can start more tasks than results you keep. Put Stream.take/2 before Task.async_stream/3 when that matters.
copied to clipboard