-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Hi,
I'm trying to wrap my head around the promise concept and I'm having some issue when dealing with HTTP GET requests.
I have a function f that calls an HTTP endpoint, decodes some JSON and returns a Swift object after doing some transformations on an intermediate type:
rgp in this case is an object containing an array of objects.
return firstly {
URLSession.shared.dataTask(.promise, with: urls.Price(forIDs: gamesIDs, country: nation, lang: nation))
}.compactMap {
try self.urls.JSONDataDecoder().decode(RawGamePrice.self, from: $0.data)
}.compactMap { rgp in
// does some transformations on rgp and then returns
}Now, I need to bolt several other HTTP calls, which should be executed for a subset of rgp components:
var proms = [Promise<[EuropeGamePrice]>]()
for (chunk) in idChunks {
proms.append(self.prices(list: chunk, nation: "IT"))
}
when(resolved: proms).done {results in
return proms.forEach { dd in
dd.value?.forEach { newPrice in
g.updateDiscountPrice(gameWith: newPrice.gameID, newPrice: newPrice.salePrice)
}
}
} .catch { error in
debugPrint("cannot fetch prices, using APIs ones", error)
}.finally {
debugPrint("finished pulling prices")
}As far as I understand, compactMap does not permit chaining other promises with then because it doesn't return a Promise itself.
What's the best way to tackle this situation?
I do not know the subset of object to work with before the second compactMap ends.
I tried embedding the when logic in the latter compactMap but since it's all async the wrapping closure returns before the when ends, and to be honest it doesn't look good enough.