Skip to content

Instantly share code, notes, and snippets.

@SebastianBoldt
Last active February 4, 2022 10:41
Show Gist options
  • Save SebastianBoldt/a363e22b879b04587c98170ab69cf7de to your computer and use it in GitHub Desktop.
Save SebastianBoldt/a363e22b879b04587c98170ab69cf7de to your computer and use it in GitHub Desktop.
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