ProposalAdd a function to the Task module that takes a list of tasks, and returns as soon as one of the tasks finishes, shuting down the other tasks.
The behaviour would pretty similar to what Javascript have with Promise.any
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/anyMotivationOne scenario that it could be useful is when we are integrating with multiple APIs (providers) of the same data, and we want only the fastest result without needing to wait for the other requests to complete.
Today I think this could be implemented with something similar to the following code:
tasks = [
Task.async(&heavy_fun_1/0),
Task.async(&heavy_fun_2/0),
Task.async(&heavy_fun_3/0)
]
receive do
{ref, result} ->
tasks
|> Enum.reject(fn task -> task.ref == ref end)
|> Enum.each(&Task.shutdown/1)
result
after
5000 ->
{:error, :timeout}
end
However that seems to be a common enough pattern to add to the standard library.
Questions- Am I missing something here and this could already be easily accomplished with the existing API?
- What should be the behaviour when the first task to complete exits?