I'm dealing with a weird API where I receive a list of IDs, and I need to request data on each of those IDs individually. I don't need to chain these requests one after the other, I just want to grab them all at once, but I'm having trouble figuring out how to do this in a clean way.
I've made the method for getting one ID, it produces a Promise<DataObject>
. How do I turn my array of IDs, into a collection of promises that will then give me [DataObject]
func fetchDataObject(_ id: Int64) -> Promise<DataObject> {
return makeURL("\(id)")
.then {
URLSession.shared.dataTask(.promise, with: $0)
}.compactMap { (data, response) -> DataObject in
// ...
return try decoder.decode(DataObject.self, from: data)
}
}
// get the list of IDs and turn them into DataObjects
func fetchNew() -> Promise<[DataObject]> { // desired output
return makeURL("all_ids").then {
URLSession.shared.dataTask(.promise, with: $0)
}.compactMap { (data, response) -> [Int64] in
// ...
return try decoder.decode([Int64].self, from: data)
}.then({ (ids) -> [DataObject] in
// now what???
})
}
I think I should be using .when
but I can't find a clear choice for which method signature... and how to create an array of promises to pass in.