Swift Concurrency
ASecurityUse `withTaskGroup` for a dynamic number of child tasks. The closure is `async`, and results are consumed with `for await`: ```swift func fetchAll(ids: [Int]) async -> [String] { await withTaskGroup(of: String?.self, returning: [String].self) { group in for id in ids { group.addTask { try? await fetchName(for: id) } } var names: [String] = [] for await name in group { if let name { names.append(name) } } return names } } ``` For operations that can fail, use `withThrowingTaskGroup` and handle...
- 584 stars
- 0 votes
- 0 copies
- 3 views
- Added September 5, 2026
Security analysis
100/100Pro scans all 16 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill swift-concurrency --agent claude-codeAre you the author of Swift Concurrency?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-swift-concurrency-b397321c)Use `withTaskGroup` for a dynamic number of child tasks. The closure is `async`, and results are consumed with `for await`:
```swift
func fetchAll(ids: [Int]) async -> [String] {
await withTaskGroup(of: String?.self, returning: [String].self) { group in
for id in ids {
group.addTask {
try? await fetchName(for: id)
}
}
var names: [String] = []
for await name in group {
if let name {
names.append(name)
}
}
return names
}
}
```
For operations that can fail, use `withThrowingTaskGroup` and handle cancellation:
```swift
func fetchAll(ids: [Int]) async throws -> [String] {
try await withThrowingTaskGroup(of: String.self, returning: [String].self) { group in
for id in ids {
group.addTask {
try Task.checkCancellation()
return try await fetchName(for: id)
}
}
var names: [String] = []
do {
for try await name in group {
names.append(name)
}
return names
} catch is CancellationError {
group.cancelAll()
throw CancellationError()
} catch {
group.cancelAll()
throw error
}
}
}
```
Call the function from an asynchronous context:
```swift
let names = try await fetchAll(ids: [1, 2, 3])
```
Task-group children are structured and remain within the parent task’s hierarchy. Use `group.cancelAll()` when one failure or cancellation should stop remaining work, and check `Task.isCancelled` or call `try Task.checkCancellation()` inside long-running loops.
For a fixed, small number of independent tasks, `async let` is usually simpler:
```swift
async let first = fetchName(for: 1)
async let second = fetchName(for: 2)
let names = try await [first, second]
```
If child tasks share mutable state, isolate that state in an `actor`. For UI updates, keep the UI type `@MainActor` or use `MainActor.run { ... }`; avoid blocking the main actor with synchronous work.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- eval-4.baseline.md
- eval-4.with-skill.md
- eval-5.baseline.md
- eval-5.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.