此代码可用于 PromiseKit v.4.5.2 。
func getReminderIds(idArray: [Int]) {
var reminderPromises: [Promise<Void>] = []
for id in idArray {
if let prom = self.delegate?.getReminders(id).then({ (reminderArray) -> Promise<Void> in
Utils.logMessage("Reminders for asset \(id): \(reminderArray)")
self.reminders[String(id)] = reminderArray
}).catch({ (err) in
self.reminders[String(id)] = nil
Utils.logMessage("Error getting reminders for asset \(id): \(err)")
}){
reminderPromises.append(prom)
}
}
_ = when(fulfilled: reminderPromises).done { results -> Void in
self.collectionView?.refreshCollection(collection: 0)
}
}
但是更新到 PromiseKit v.6.8.4 之后,我得到了错误 “无法将类型'PMKFinalizer'的值转换为预期的参数类型'Promise'” 在这一行:
reminderPromises.append(prom)
答案 0 :(得分:4)
struct Reminder {
let id: Int
let value: [String: Any]
}
func getReminderIds(idArray: [Int]) {
var reminderPromises: [Promise<Reminder>] = []
for id in idArray {
reminderPromises.append(getReminders(id))
}
_ = when(fulfilled: reminderPromises).done { results -> Void in
for item in results {
print(item.id)
print(item.value)
}
}
}
func getReminders(_ id: Int) -> Promise<Reminder> {
// TODO network request or database request
return Promise { $0.fulfill(Reminder(id: id, value: [:])) }
}