How to handle two async API responses using combine?
I have two async calls to fetch data from the server, but I want them to handle them as a single response, also want to handle errors for each response.
Here I have two methods m1()
, m2()
each can throw different types of errors.
Wants to wait to get a response of both and show an error message based on its error type. If there is no error, continue with the flow. Which operator do we have to use? I tried with Publishers.Zip
& Publishers. Map
not able to handle errors.
import Combine
enum Error1: Error {
case e1
}
enum Error2: Error {
case e2
}
func m1() -> Future<Bool, Error1> {
return Future { promise in
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
promise(.failure(.e1))
}
}
}
func m2() -> Future<String, Error2> {
return Future { promise in
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
promise(.success("1"))
}
}
}
Help would be greatly appreciated.!! Thank you.