## Background

I have an enumerable over which I fold and call Task.async based on the 
data in the enumerable. I then Task.yield_many over the list of tasks, and 
use Enum.zip to correlate the original enumerable with the results.

I have a case where, during the fold I find that an entry is invalid for 
running Task.async. It would be convenient to create an "already completed" 
Task that contains an error result. For now, I'm still using Task.async to 
basically return an :error tuple, which of course starts and links a 
process.

Of course, I could work around this by using maps, etc.

I tried using %Task{} to create a "dummy" but calling Task.yield_many with 
such an entry always blocks until the timeout.

## Proposal

Add Task.completed/1 that creates an "already completed" Task that can then 
be awaited / yielded to return the result used when completed/1 was called:

```elixir
task = Task.completed({:error, :boom})
```

Awaiting or yielding on such a task returns the result immediately without 
invoking a process.

## Other

.NET has the following to achieve this behavior, for instance:

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

Thanks for your consideration! If approved I would gladly implement this.
Luke

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/d491f792-5255-480f-941d-2daa8f1a056cn%40googlegroups.com.

Reply via email to