0

This code worked with 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)
    }

}

But after updating to PromiseKit v.6.8.4 I get the error "Cannot convert value of type 'PMKFinalizer' to expected argument type 'Promise'" in this line:

reminderPromises.append(prom)
maxwell
  • 3,788
  • 6
  • 26
  • 40

1 Answers1

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: [:])) }
    }
Vicaren
  • 654
  • 4
  • 12
  • 1
    Yes, change then to done and remove this -> Promise, Done blocks never return – Vicaren Jun 26 '19 at 10:40
  • Now I have error "Cannot convert value of type '(_) -> Promise' to expected argument type '([Int]) -> Void'" in this line: if let prom = self.delegate?.getReminders(assetId: ID).done({ (reminders) -> Promise in – maxwell Jun 26 '19 at 10:44
  • 1
    if let prom = self.delegate?.getReminders(assetId: ID).done({ (reminders) in can you try this – Vicaren Jun 26 '19 at 10:44
  • Now I have the same error as in my question "Cannot convert value of type 'PMKFinalizer' to expected argument type 'Promise'" in this line reminderPromises.append(prom) :) – maxwell Jun 26 '19 at 10:46
  • When I try to set reminders, I get an error "Cannot convert value of type 'Promise<[Int]>?' to expected argument type '[Int]?'". Because reminders have Promise<[Int]>? type. I tried this: func getReminders(_ id: Int) -> Promise { // TODO network request or database request let reminders = self.delegate?.getReminders(assetId: id) return Promise { $0.fulfill(Reminder(id: id, value: reminders ?? [])) } } – maxwell Jun 26 '19 at 12:07
  • You can set reminders in for loop ( i printed id and value ) if remindes count less than zero you can set nil too. Shortly create all instance of promises in array than append array without set done, catch or finally block. You can get all results in when closure with in array after. You can set done catch finally block to when instance – Vicaren Jun 26 '19 at 12:34