note that it is relatively painful to achieve this "parallel" sequence of execution with callbacks. i wish it would also be somehow possible with async/await, otherwise we'd have to switch between the two worlds.
error handling omitted:
func getFirstResult(execute: @escaping (Result) -> Void) {
...
}
func getSecondResult(execute: @escaping (Result) -> Void) {
...
}
func combineResults(_ r1: Resut, _ r2: Result, @escaping (Result) -> Void) {
...
}
func getTotalResult(execute: @escaping (Result) -> Void) {
var result1: Result? = nil
var result2: Result? = nil
func combineResultsIfReady() {
if let r1 = result1, let r2 = result2 {
combineResults(r1, r2, execute: execute)
}
}
getFirstResult {
self.result1 = $0
combineResultsIfReady()
}
getSecondResult {
self.result2 = $0
combineResultsIfReady()
}
}
and sequential execution (when needed) is easier. again, no error handling here:
getFirstResult { r1 in
getSecondResult { r2 in
combineResults(r1, r2, execute: execute)
}
}