With Swift
, I am trying to incorporate promises into my code with PromiseKit
for the first time and am having difficulty understanding syntactically how to make this work.
My goal is to pipeline requests to Firebase
for data and execute another method once an appended array is fulfilled with all the returned data. It seems the promise
is resolved and the final method is run before the array is set with the data. I believe I should be using "when", but admittedly have trouble understanding the documentation.
Abbreviated, relevant code below:
getPosts(ids).done {
runningFinalMethod(self.arr)
}
func getPosts(_ ids: [String]) -> Promise<Void> {
return Promise { seal in
for id in ids {
db.collection("data").whereField("id", isEqualTo: id).getDocuments(completion: { (dataForId, error) in
appendData(dataForId)
})
}
seal.fulfill(())
}
}
func appendData(_ dataForId: [data]) {
arr.append(contentsOf: dataForId)
}