/TaskGroupsResultBuilder

A @resultBuilder to use to bundle tasks together.

Primary LanguageSwiftMIT LicenseMIT

TaskGroupsResultBuilder

Make sure to read my article on TaskGroups before exploring this project: Task Groups in Swift explained with code examples.

This project demonstrates how you can use a @resultBuilder in combination with TaskGroup.

Example

let photoURLs = try await listPhotoURLs(inGallery: "Amsterdam Holiday")
let images = try await withThrowingTaskGroup {
    for photoURL in photoURLs {
        Task { try await downloadPhoto(url: photoURL) }
    }
}

Or:

func taskGroupExampleThree() async {
    let names = await withTaskGroup {
        Task {
            await asyncOperation(name: "Antoine")
        }
        Task {
            await asyncOperation(name: "Maaike")
        }
        Task {
            await asyncOperation(name: "Sep")
        }
    }
    print("Received: \(names)") // Received: ["Antoine", "Maaike", "Sep"]
}