Skip to content

Conversation

superhawk610
Copy link
Contributor

This PR implements the new Task function Task.completed/1 as proposed in the elixir-lang group.

This function models a task that immediately completes with a known result. As documented in the @doc block, it's a useful convenience when working with groups of asynchronous tasks where some inputs may have a result that can be determined without actually running the task. Here's a quick example:

def process(data) do
  tasks =
    for entry <- data do
      if invalid_input?(entry) do
        Task.completed({:error, :invalid_input})
      else
        Task.async(fn -> further_process(entry) end)
      end
    end

  Task.await_many(tasks)
end

Here are a couple links to prior art in .NET:

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.completedtask?view=net-5.0

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.fromresult?view=net-5.0

Credit for the idea goes to @lukebakken.

Copy link

@lukebakken lukebakken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@josevalim josevalim merged commit 032a45d into elixir-lang:master Jul 2, 2021
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@superhawk610 superhawk610 deleted the feat/task-completed-v2 branch July 2, 2021 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants