It sounds good to me. A small but likely welcome change. A PR to further explore this is welcome!
On Thu, Jul 1, 2021 at 12:26 AM Aaron Ross <superhawk...@gmail.com> wrote: > +1 > > I've encountered this in a similar context - I have one overall task that > will spawn some number of data fetching tasks, but in some cases I know > that the data fetch call will return no results so I stub the task with > `Task.async(fn -> {:ok, []} end)`. The proposed `Task.completed/1` would be > a great, more semantic replacement and has the benefit of not spawning an > unneeded process. > > On Wednesday, June 30, 2021 at 3:22:07 PM UTC-7 Luke Bakken wrote: > >> ## 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/d12a9c8c-6441-48d7-9ebe-1194abb86f30n%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/d12a9c8c-6441-48d7-9ebe-1194abb86f30n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAGnRm4%2Brzs%3DX9QsiTOpBPQ3SO%2B4X1RWZtn4CqOj0-GJHF0UoTw%40mail.gmail.com.