ELIXIR / CHAPTER 11
Task results, links, and time budgets
AdvancedDefine how concurrent work completes and fails.
By the end of this chapter
- Await the result of a task explicitly
- Distinguish linking from result collection
- Use bounded task coordination policies
The idea
Task.async starts work in another process and returns a task handle; Task.await retrieves its result with a timeout. The task is linked to its caller, so failure propagation is part of the contract rather than an incidental implementation detail. Do not assume that wrapping await in an ordinary rescue block turns every linked-process failure into a recoverable value.
Use an appropriate supervisor and unlinked task strategy when the caller must observe failures without being taken down by the task. For many inputs, async_stream can bound concurrency and expose tagged outcomes, with ordering and timeout options that should be chosen deliberately. Concurrency does not guarantee faster work: include process coordination and external rate limits in the design.
task = Task.async(fn -> Enum.sum([2, 4, 6]) end)
result = Task.await(task)
IO.inspect(result)12
Await obtains the task’s completed result. The example has no external effects or failure, but real callers must choose a failure policy.
Try it yourself
Design a bounded concurrent report fetcher with explicit timeout and per-task error handling.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
CHECK YOUR UNDERSTANDING