I want to perform multiple tasks concurrently. In Javascript, I would do:
async function cook_an_egg() {}
async function take_shower() {}
async function call_mum() {}
await Promise.all([cook_an_egg(), take_shower(), call_mum()])
How do I achieve Promise.all
in Elixir Task module?
From documentation, seems you can only await
1 task; define 1 function inside each task
; and apply only the same function to multiple items with async_stream
.
You can map await
function to a list of task refs.
Something like
tasks = Enum.reduce(0..9, [], fn _, acc ->
[Task.async(&any_job/0) | acc]
end)
Enum.map(tasks, &Task.await/1)