SE-0304: Structured Concurrency

If there's room for API naming changes I would propose:

static func withGroup<Element, GroupResult>(
  of: Element.Type,
  returning returnType: GroupResult.Type = GroupResult.self,
  body: (inout Task.Group<Element>) async throws -> GroupResult
) async rethrows -> GroupResult { ... }

Task.Group<TaskResult> conforms to AsyncSequence and TaskResult is its Element type. "A task group of Ints" as an async sequence of Ints.

await Task.withGroup(of: Int.self) { group in
  await group.add { 1 }

  for try await int in group { ... }
}
1 Like