Created
October 29, 2024 16:18
-
-
Save 0xMarK/5be565d29d525d89311e0934570461c8 to your computer and use it in GitHub Desktop.
Complicated Task Group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try await withThrowingTaskGroup(of: LoginTaskResult.self) { group in | |
group.addTask(operation: fetchLocation) | |
group.addTask(operation: askForUserPermissions) | |
group.addTask(priority: .high) { | |
return .client(try await APIClient.connect()) | |
} | |
var client: APIClient! | |
for try await result in group { | |
if case LoginTaskResult.client(let api) = result { | |
client = api | |
break | |
} | |
} | |
let tracer = client.traceEvent | |
group.addTask(operation: client.logUserIn) | |
group.addTask { | |
try await tracer(.login) | |
} | |
try await group.waitForAll() | |
_ = try await tracer(.completedLoginSequence) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment