Last active
February 4, 2022 10:41
-
-
Save SebastianBoldt/a363e22b879b04587c98170ab69cf7de to your computer and use it in GitHub Desktop.
This file contains 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
func getDocuments(range: Range<Int>) async throws -> [Data] { | |
// 1. | |
let documents = try await withThrowingTaskGroup(of: Data.self, returning: [Data].self,body: { group -> [Data] in | |
// 2. | |
for i in range { | |
group.addTask(priority: .high, operation: { () -> Data in | |
return try await getDocument(fileName: "Document-\(i)") | |
}) | |
} | |
var documents = [Data]() | |
// 3. | |
for try await document in group { | |
documents.append(document) | |
} | |
// 4. | |
return documents | |
}) | |
return documents | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment