I have 2 API endpoints; the latter depends on the result of the first.
The first end point is /api/v1/regions/
, which returns a list of region JSON like so
{
region_id: 1,
mayor_id: 9
},
{
region_id: 1,
mayor_id: 10
},
The second end point is /api/v1/mayor/<id>/
, which returns a JSON about the mayor. My workflow right now is to make the the first API call to get all the regions, then I want to make a bunch of API calls to the /mayor/
endpoint based on the IDs I get from the first end point. So in this example, I'd like to make 2 more calls:
/api/v1/mayor/9/
/api/v1/mayor/10/
I've already set up 2 functions to make each API call and successfully got the JSON back for each.
func fetchRegions() -> Promise<[Region]> {
}
func fetchMayor(id: String) -> Promise<Mayor> {
}
Now I'd like to see how I could chain all of these together. This is what I have so far:
var fetchedRegions: [Region] = []
firstly {
fetchRegions()
}.then { regions in
fetchedRegions = regions
}.then {
for r in fetchedRegions {
self.fetchMayor(id: r.mayor_id).then { mayor in
print(mayor)
}.catch { error in
}
}
}.catch { error in // Error: Missing return in a closure expected to return 'AnyPromise'
print(error)
}