I would like to compare the results of two separate server calls and am struggling with how to do this using the closure format.
For example, if a weather api will return the temperature for one city at a time and I want to compare the temperatures in two cities, I'm struggling with how to wait for both calls to complete.
The code I am using, adapted from a single server call, can wait for the first result. However, I can't figure out how to then wait for the second result.
Here is my code:
func compareWeather (onecity:String, anothercity:String,completion:@escaping (_ response:String)->()){
let city1:String = "Boston"
let city2:Strng = "Palo Alto"
self.getWeatherForCity(city: city1){//open 2
answer in
let temp1:String = answer
}
self.getWeatherForCity(city: city2){//open 2
answer in
let temp2:String = answer
}
//THIS DOES NOT WORK Since it is called before we here back from APIS BUT THIS IS WHAT I WANT TO accomplish
completion("the temperature in Boston is ",temp1, "but the temperature in Palo Alto is ",temp2)
}