Ambiguous Use of Recover Error While Using Promisekit

Invalid conversion of recover block, PromiseKit

You should return Promise to chain correctly, like this :

self.supplierController
.update(supplier: supplier)
.recover { error -> Promise<Supplier> in
let supplier: Supplier = supplier
guard (error as NSError).code == 405 else {
throw error
}

return .value(supplier)
}
.done { (_: Supplier) in
changeCompanyIdAndAppendMessage()
}

source: https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md

Unable to use then on ios swift promisekit

You have to return a promise when you do .then because until .done you are not finished. I don't see any issue in saving realm data in .done as your method has no return type.

Incase you still want to use .then, it can be done this way.

Translator().translators().then({ data -> Promise<[Translator]> in
return Promise.value(data)
}).done({ data in
// Save to realm
}).catch({ error in
print(error.localizedDescription)
})

Using PromiseKit I want to make multiple API calls

So I managed to achieve the batch calls I wanted using PromiseKit and handling all errors together:

private func batchDeleteItem(with itemId: UInt) -> Promise<(UInt, AppError?)> {
return Promise { seal in
firstly {
AppConfigManager.shared.getApiUrl(path: Constants.Api.deleteItemRequestPath, itemId)
}.then { url in
ApiManager.shared.request(url, method: .delete)
}.done { _ in
seal.fulfill((itemId, nil))
}.catch { error in
guard let appError = error as? AppError else {
seal.reject(error)
return
}
seal.fulfill((itemId, appError))
}
}
}

I call this method in a map, and in order to collect the ids of items that couldn't be deleted, I treat the catch with fulfill as wel - rejecting only server side error (this is what I needed for my case) :

return Promise { seal in

let promises = ids.map { itemId in
batchDeleteNode(with: itemId)
}

when(fulfilled: promises).done { results in
seal.fulfill(results)
}.catch { error in
seal.reject(error)

}
}

And to have all the errors grouped and waited for I use when(fulfilled: promises).done {...}

I'm not saying it's perfect but seems to work very well, and I learned a lot in the process regarding PromiseKit.

PromiseKit Framework - Ambiguous reference to member 'then()'

Try this code below. The promise thing should work. Modify it upon your needs.

gnEndpoints?.paymentToken(for: gnCreditCard).then({ token -> () in
if let token = token as? GNPaymentToken {
print(token)
}
}, { (error) -> () in
// catch your error
})


Related Topics



Leave a reply



Submit