Parallel IO
One of the most powerful uses of tasks is running multiple IO operations at the same time.
def runParallel : IO Unit := do
let t1 ← IO.asTask do
IO.sleep 1000
return "A"
let t2 ← IO.asTask do
IO.sleep 1000
return "B"
IO.println "Waiting for both tasks..."
let r1 ← IO.wait t1
let r2 ← IO.wait t2
IO.println s!"Results: T₁ = {r1.toOption.getD ""},
T₂ = {r2.toOption.getD ""}"
In this example, the total wait time is approximately 1 second, even though we performed two 1-second sleeps, because they ran in parallel.