Record awaited async task completions and report the coordination state.

awaited task value A coordination report can record how many async values a section awaited to completion. Each loop step awaits one `async` function call, so its value is fully resolved before the next step runs, and the completed counter advances only after the result arrives. With no tasks the section stays idle, with a single awaited task it reports one completion, and with several awaited tasks it reports a batched result. The status line summarizes the mode while the total confirms every awaited value was folded in.

Task Value Coordination Report

tasks
task_value_coordination_report.swift
Replay: real traced execution (multi-file project)
func produce(_ id: Int) async -> Int {
    return (id + 1) * 10
}

let tasks = 1
var completed = 0
var total = 0

for i in 0..<tasks {
    let value = await produce(i)
    completed += 1
    total += value
}

var status = "single"
if completed == 0 {
    status = "idle"
}
if completed > 1 {
    status = "batched"
}

let line = "tasks=\(tasks) completed=\(completed) total=\(total) \(status)"

print(line)
func produce(_ id: Int) async -> Int {
    return (id + 1) * 10
}

let tasks = 0
var completed = 0
var total = 0

for i in 0..<tasks {
    let value = await produce(i)
    completed += 1
    total += value
}

var status = "single"
if completed == 0 {
    status = "idle"
}
if completed > 1 {
    status = "batched"
}

let line = "tasks=\(tasks) completed=\(completed) total=\(total) \(status)"

print(line)
func produce(_ id: Int) async -> Int {
    return (id + 1) * 10
}

let tasks = 2
var completed = 0
var total = 0

for i in 0..<tasks {
    let value = await produce(i)
    completed += 1
    total += value
}

var status = "single"
if completed == 0 {
    status = "idle"
}
if completed > 1 {
    status = "batched"
}

let line = "tasks=\(tasks) completed=\(completed) total=\(total) \(status)"

print(line)
  1. tasks ← 1, completed ← 0, total ← 0

    5let tasks→ 1 = 1  //@tasks=0, 26var completed→ 0 = 07var total→ 0 = 0
  2. for i in 0..<tasks

    9for i0 in 0..<tasks1 {10    let value = await produce(i0)11    completed += 1
  3. func produce(_ id: Int) async -> Int

    1func produce(_ id0: Int) async -> Int {2    return (id0 + 1) * 103}
  4. value ← 10, completed ← 1, total ← 10

    9for i in 0..<tasks {10    let value→ 10 = await produce(i0)11    completed→ 1 += 112    total→ 10 += value1013}
  5. status ← single, line ← tasks=1 completed=1 total=10 single

    15var status→ single = "single"16if completed == 0 {17    status = "idle"18}19if completed > 1 {20    status = "batched"21}2223let line→ tasks=1 completed=1 total=10 single = "tasks=\(tasks1) completed=\(completed1) total=\(total10) \(statussingle)"2425print(linetasks=1 completed=1 total=10 single)
    outputtasks=1 completed=1 total=10 single
  1. tasks ← 0, completed ← 0, total ← 0, status ← single

    5let tasks→ 0 = 06var completed→ 0 = 07var total→ 0 = 089for i in 0..<tasks {10    let value = await produce(i)11    completed += 112    total += value13}1415var status→ single = "single"16if completed == 0 {
  2. if completed == 0

    15var status = "single"16if completed0 == 0 {17    status = "idle"18}
  3. line ← tasks=0 completed=0 total=0 idle

    23let line→ tasks=0 completed=0 total=0 idle = "tasks=\(tasks0) completed=\(completed0) total=\(total0) \(statusidle)"2425print(linetasks=0 completed=0 total=0 idle)
    outputtasks=0 completed=0 total=0 idle
  1. tasks ← 2, completed ← 0, total ← 0

    5let tasks→ 2 = 26var completed→ 0 = 07var total→ 0 = 0
  2. for i in 0..<tasks

    pass 1 of 2
    9for i0 in 0..<tasks2 {10    let value = await produce(i0)11    completed += 1
  3. func produce(_ id: Int) async -> Int

    pass 1 of 2
    1func produce(_ id0: Int) async -> Int {2    return (id0 + 1) * 103}
  4. value ← 10, completed ← 1, total ← 10

    9for i in 0..<tasks {10    let value→ 10 = await produce(i0)11    completed→ 1 += 112    total→ 10 += value1013}
  5. for i in 0..<tasks

    pass 2 of 2
    9for i1 in 0..<tasks2 {10    let value = await produce(i1)11    completed += 1
  6. func produce(_ id: Int) async -> Int

    pass 2 of 2
    1func produce(_ id1: Int) async -> Int {2    return (id1 + 1) * 103}
  7. value ← 20, completed ← 2, total ← 30

    9for i in 0..<tasks {10    let value→ 20 = await produce(i1)11    completed→ 2 += 112    total→ 30 += value2013}
  8. status ← single

    15var status→ single = "single"16if completed == 0 {
  9. if completed > 1

    18}19if completed2 > 1 {20    status = "batched"21}
  10. line ← tasks=2 completed=2 total=30 batched

    23let line→ tasks=2 completed=2 total=30 batched = "tasks=\(tasks2) completed=\(completed2) total=\(total30) \(statusbatched)"2425print(linetasks=2 completed=2 total=30 batched)
    outputtasks=2 completed=2 total=30 batched